Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2017
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. ;; product with break and O(1) case for a zero operand
  2.  
  3. ;; call/cc
  4. (define (product-cc ls)
  5. (call-with-current-continuation
  6. (lambda (break)
  7. (cond ((null? ls) 1)
  8. ((= (car ls) 0) (break 0))
  9. (else (* (car ls) (product-cc (cdr ls))))))))
  10.  
  11. ;; continuation-passing style
  12. (define (product-cps ls k break)
  13. (cond ((null? ls) (k 1))
  14. ((= (car ls) 0) (break 0))
  15. (else (product-cps (cdr ls) (lambda (x) (k (* (car ls) x))) break))))
  16.  
  17. (define (product-cps2 ls k)
  18. (let ((break k))
  19. (let f ((ls ls) (k k))
  20. (cond ((null? ls) (k 1))
  21. ((= (car ls) 0) (break 0))
  22. (else (f (cdr ls)
  23. (lambda (x)
  24. (k (* (car ls) x)))))))))
  25.  
  26. (define (product ls)
  27. (product-cps ls (lambda (x) x) (lambda (x) x)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement