Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scheme 方案闭合函数_Scheme_Closures - Fatal编程技术网

Scheme 方案闭合函数

Scheme 方案闭合函数,scheme,closures,Scheme,Closures,我得到了这段Scheme代码,并负责将代码翻译成Perl (define (makecounter) (let ((x 0)) (lambda () (begin (set! x (+ x 1)) x)))) 我知道这应该是一个闭包函数,但我不太明白它应该做什么。有什么想法吗?提示: (define c1 (makecounter)) (define c2 (makecounter)) (c1) => 1 (c1) => 2 (c1) => 3 (c2) => 1 (

我得到了这段Scheme代码,并负责将代码翻译成Perl

(define (makecounter)
 (let ((x 0)) (lambda () (begin (set! x (+ x 1)) x))))
我知道这应该是一个闭包函数,但我不太明白它应该做什么。有什么想法吗?

提示:

(define c1 (makecounter))
(define c2 (makecounter))
(c1)
=> 1
(c1)
=> 2
(c1)
=> 3
(c2)
=> 1
(c2)
=> 2
(c1)
=> 4
提示:


看起来它返回的函数会增加一个变量看起来它返回的函数会增加一个变量正是我所需要的!谢谢,这正是我需要的!非常感谢。