如何将数据传递给 call/cc?
how to pass data to call/cc?
我尝试实现接受原子列表的函数,returns 只是给定列表的子部分。子部分是特定标记之后的所有内容。为此,我尝试使用 call-with-current-continuation
。
这是我的方法:
(define rest
(lambda (a lat)
(call-with-current-continuation
(lambda (res)
(letrec ((r (lambda (l)
(cond
((null? l) ())
((eq? (car l) a) (res (cdr l)))
(else (r (cdr l)))))))
(r lat))))))
如您所见,我尝试将数据传递给 continuation
((eq? (car l) a) (res (cdr l)))
但我总是使用这种方法返回 ()
。但是,如果我像 ((eq? (car l) a) (apple tea)
这样硬编码值,它就可以正常工作。
我真的被困住了,我需要帮助。
编辑:我找到了解决方案而不是 (res (cdr l))
我必须通过 (res (r (cdr l)))
真正的错误不是我如何调用 continuation
,而是 (else (r (cdr l)))))))
我应该写成:
(else (cons (car l)
(r (cdr l))))))))
您的代码在 MIT-Scheme 9.2 中运行良好:
MIT/GNU Scheme running under OS X
Type `^C' (control-C) followed by `H' to obtain information about interrupts.
Copyright (C) 2014 Massachusetts Institute of Technology
This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.
Image saved on Wednesday February 24, 2016 at 8:07:52 PM
Release 9.2 || Microcode 15.3 || Runtime 15.7 || SF 4.41 || LIAR/C 4.118 || Edwin 3.116
1 ]=> (define rest
(lambda (a lat)
(call-with-current-continuation
(lambda (res)
(letrec ((r (lambda (l)
(cond
((null? l) ())
((eq? (car l) a) (res (cdr l)))
(else (r (cdr l)))))))
(r lat))))))
;Value: rest
1 ]=> (rest 'c '(a b c d))
;Value 2: (d)
1 ]=> (rest 'c '(a b c d e))
;Value 3: (d e)
1 ]=> (rest 'c ())
;Value: ()
我的最佳猜测是您正在处理对象,其中 eq?
是不恰当的比较过程。
FWIW,这是一个较短的实现:
(define (rest a lat)
(call-with-current-continuation
(lambda (res)
(let r ((l lat))
(if (null? l)
'()
((if (eq? (car l) a) res r) (cdr l)))))))
我尝试实现接受原子列表的函数,returns 只是给定列表的子部分。子部分是特定标记之后的所有内容。为此,我尝试使用 call-with-current-continuation
。
这是我的方法:
(define rest
(lambda (a lat)
(call-with-current-continuation
(lambda (res)
(letrec ((r (lambda (l)
(cond
((null? l) ())
((eq? (car l) a) (res (cdr l)))
(else (r (cdr l)))))))
(r lat))))))
如您所见,我尝试将数据传递给 continuation
((eq? (car l) a) (res (cdr l)))
但我总是使用这种方法返回 ()
。但是,如果我像 ((eq? (car l) a) (apple tea)
这样硬编码值,它就可以正常工作。
我真的被困住了,我需要帮助。
编辑:我找到了解决方案而不是 (res (cdr l))
我必须通过 (res (r (cdr l)))
真正的错误不是我如何调用 continuation
,而是 (else (r (cdr l)))))))
我应该写成:
(else (cons (car l)
(r (cdr l))))))))
您的代码在 MIT-Scheme 9.2 中运行良好:
MIT/GNU Scheme running under OS X
Type `^C' (control-C) followed by `H' to obtain information about interrupts.
Copyright (C) 2014 Massachusetts Institute of Technology
This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.
Image saved on Wednesday February 24, 2016 at 8:07:52 PM
Release 9.2 || Microcode 15.3 || Runtime 15.7 || SF 4.41 || LIAR/C 4.118 || Edwin 3.116
1 ]=> (define rest
(lambda (a lat)
(call-with-current-continuation
(lambda (res)
(letrec ((r (lambda (l)
(cond
((null? l) ())
((eq? (car l) a) (res (cdr l)))
(else (r (cdr l)))))))
(r lat))))))
;Value: rest
1 ]=> (rest 'c '(a b c d))
;Value 2: (d)
1 ]=> (rest 'c '(a b c d e))
;Value 3: (d e)
1 ]=> (rest 'c ())
;Value: ()
我的最佳猜测是您正在处理对象,其中 eq?
是不恰当的比较过程。
FWIW,这是一个较短的实现:
(define (rest a lat)
(call-with-current-continuation
(lambda (res)
(let r ((l lat))
(if (null? l)
'()
((if (eq? (car l) a) res r) (cdr l)))))))