Advertisement
Guest User

Untitled

a guest
Jul 10th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 0.86 KB | None | 0 0
  1. ;; Quite ugly, but that's a simple use of call/cc.
  2.  
  3. (define (mul-with-shortcut l)
  4.   (display
  5.     (list "result is "
  6.           (call/cc
  7.             (lambda (escape)
  8.               (let loop ((l l))
  9.                 (if (null? l)
  10.                   1
  11.                   (let ((head (car l))
  12.                         (tail (cdr l)))
  13.                     (if (zero? head)
  14.                       (begin
  15.                         (display "exit!")
  16.                         (newline)
  17.                         (escape 0)) ;; you multiplied by zero. No need to continue multiplying. Just return 0.
  18.                       (begin
  19.                         (for-each display `("multiplying by " ,head #\newline))
  20.                         (* head (loop tail))))))))))))
  21.  
  22. (define (test)
  23.   (mul-with-shortcut '(1 2 3 4 5))
  24.   (newline)
  25.   (mul-with-shortcut '(1 2 0 4 5))
  26.   (newline))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement