Skip to content

Instantly share code, notes, and snippets.

@valvallow
Created April 1, 2010 16:51
list-product, let/cc
;; list-product
;; (list-product '(1 2 3 4 5))
;; -> 120
(define (list-product l)
(let/cc skip
(fold (lambda (e acc)
(if (zero? e)
(skip 0)
#?=(* e acc)))
1 l)))
(list-product '(1 2 3 4 5))
;; #?="(stdin)":22:(* e acc)
;; #?- 1
;; #?="(stdin)":22:(* e acc)
;; #?- 2
;; #?="(stdin)":22:(* e acc)
;; #?- 6
;; #?="(stdin)":22:(* e acc)
;; #?- 24
;; #?="(stdin)":22:(* e acc)
;; #?- 120
;; 120
(list-product '(5 6 7 8 9 0 1 2 3 4 5))
;; #?="(stdin)":22:(* e acc)
;; #?- 5
;; #?="(stdin)":22:(* e acc)
;; #?- 30
;; #?="(stdin)":22:(* e acc)
;; #?- 210
;; #?="(stdin)":22:(* e acc)
;; #?- 1680
;; #?="(stdin)":22:(* e acc)
;; #?- 15120
;; 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment