简体   繁体   中英

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 ?

(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)))]))))))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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