编写一个带有两个参数的forAll过程:一个序列的开始和结束值,并将给定过程应用于该序列 [英] Writing a forAll procedure that takes two arguments: the start and end values of a series and applies a given procedure to that series

查看:92
本文介绍了编写一个带有两个参数的forAll过程:一个序列的开始和结束值,并将给定过程应用于该序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个包含两个参数的forAll过程:系列的开始和结束值.产生的闭包也需要两个参数:一个应用于系列中所有元素的运算以及一个初始值.

I am trying to write a forAll procedure that takes two arguments: the start and end values of a series. The resulting closure expects two arguments as well: an operation to apply to all elements in the series, as well as an initial value.

这就是我所拥有的,我似乎丢失了一些东西,或者我不了解闭包背后的概念.

This is what I have and, I seem to be missing something or I don't understand the concept behind closures.

(define (forAll n m)
   (if (>= n m) '()
   (forAll (+ n 1) m))

 (lambda (op start) (op start n m))
       )

推荐答案

这看起来像是将现有功能合并为新功能"的练习.
首先编写一个生成数字列表的函数(如果尚未完成的话).
使用它来实现此功能.
回顾一下您最近学到的内容,看看是否已经有一个函数执行与闭包应该执行的操作类似的功能.

This looks like a "combine existing functions into new ones" exercise.
Start with writing a function that generates the list of numbers if you haven't already done that.
Use that to make this function.
Review what you've learned recently and see if you've already got a function that does something similar to what the closure should do.

您最终得到的结果可能看起来像这样:

What you end up with will probably look like this:

(define (forAll n m)
    (lambda (op start)
        (... code that builds a list and computes the result ...)))

这篇关于编写一个带有两个参数的forAll过程:一个序列的开始和结束值,并将给定过程应用于该序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆