Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;; Quite ugly, but that's a simple use of call/cc.
- (define (mul-with-shortcut l)
- (display
- (list "result is "
- (call/cc
- (lambda (escape)
- (let loop ((l l))
- (if (null? l)
- 1
- (let ((head (car l))
- (tail (cdr l)))
- (if (zero? head)
- (begin
- (display "exit!")
- (newline)
- (escape 0)) ;; you multiplied by zero. No need to continue multiplying. Just return 0.
- (begin
- (for-each display `("multiplying by " ,head #\newline))
- (* head (loop tail))))))))))))
- (define (test)
- (mul-with-shortcut '(1 2 3 4 5))
- (newline)
- (mul-with-shortcut '(1 2 0 4 5))
- (newline))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement