如何将数据传递给call / cc?

时间:2016-02-24 17:22:33

标签: scheme continuations

我尝试实现获取原子列表的函数并仅返回给定列表的子部分。子部分是特定标记之后的所有内容。我尝试将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))))))))

1 个答案:

答案 0 :(得分:1)

您的代码在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)))))))