【发布时间】:2014-03-23 05:59:50
【问题描述】:
目前,我正在学习 Scheme 语言。 我对如何使用 call-with-current-continuation 感到困惑。(call/cc) 为了更好地理解它,我为非本地出口编写了一个示例代码。 但它不能正常工作。
有人知道为什么吗?任何帮助,将不胜感激。 提前致谢
[示例代码]
(define (product ls)
(call/cc
(lambda (return)
(cond
((null? ls ) =>
(begin
(display "list end")
(newline)
1)) ;; NG
;;(return 1)) ;; OK
((not (number? (car ls))) =>
(begin
(display "not number")
(newline)
(return 0)))
(else =>
(begin
(display (car ls))
(newline)
(* (car ls) (product (cdr ls)))))))))
[复制输出]
gosh> (product '(1 2 a 3)) ; it works as I expected.
==> 1
==> 2
==> not number
==> 0 (return)
gosh> (product '(1 2 3)) ;; it doesn't work as I expected. I expect 6 as return value.
==> 1
==> 2
==> 3
==> list end
*** ERROR: invalid application: (1 #t)
【问题讨论】:
标签: scheme