简体   繁体   English

方案:避免调用程序以获取延续?

[英]Scheme: Avoid to invoke a procedure in order to grab a continuation?

So I had this code below : 所以我在下面的代码:

(define escape
(lambda ()
   (set! halt (call/cc (lambda (k) k)))
   0))

(define multiply
 (lambda (l)
   (if (null? l)
      1
       (if (zero? (car l))
          (halt halt)
            (* (car l) (multiply (cdr l)))))))

and I had to redesign "multiply" to avoid "escape" and grab a value of the continuation,so that "halt" returns the value of the answer I come up with this solution how is it look? 并且我不得不重新设计“乘”以避免“逃脱”并获取延续的值,以便“暂停”返回答案的值。我想出的解决方案看起来如何?

(define multiply
(lambda () 
(let ((result (call/cc (lambda (k) (set! halt k) '())))) 
  (if (procedure? halt)
   (tester 
     ((multiply (lambda (k)
       (if (= k 0) (halt k) 
           (multiply (* k 1))))))
     (result 1))
   halt))))

Are you just trying to write an early-returning multiply function using call/cc ? 您是否只是在尝试使用call/cc编写返回早期的乘法函数?

(define multiply
    (lambda (l)
      (call/cc
        (lambda (k)
          (let loop ([l l])
            (cond
              [(null? l) 1]
              [(= 0 (car l)) (k 0)]
              [else (* (car l) (multiply (cdr l)))]))))))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM