方案分配 [英] Scheme assignment

查看:35
本文介绍了方案分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我每次得到值 10 时评估以下表达式.

When I evaluate the following expression every time I get the value 10.

(((lambda (x) (lambda () (set! x (+ x 10)) x)) 0)) 

然而,我只是通过使用名称抽象上述过程并在每次值增加 10 时调用 foo 来修改!!

However I just modify by abstracting the above procedure with a name and call foo every time the value increments by 10!!

(define foo ((lambda (x) (lambda () (set! x (+ x 10)) x)) 0))

有人可以解释一下吗?

推荐答案

您正在调用的函数是一个计数器,每次调用时都会返回一个大 10 的数字.

The function you are calling is a counter that returns a number 10 higher every time it's called.

在第一种情况下,每次,您都在创建一个新函数,然后立即调用它一次,然后丢弃该函数.所以每次,你都是第一次调用这个计数器的一个新实例,所以它应该返回 10.

In the first case, every time, you are creating a new function and then immediately calling it once and then discarding the function. So every time, you are calling a new instance of this counter for the first time, so it should return 10.

在第二种情况下,您创建一次函数并将其分配给一个变量并重复调用同一个函数.由于您正在调用相同的函数,它应该返回 10, 20, ...

In the second case, you create the function once and assign it to a variable and call that same function repeatedly. Since you are calling the same function, it should return 10, 20, ...

这篇关于方案分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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