1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- (import (except (rnrs base))
- (only (guile)
- lambda* λ
- display
- simple-format))
- (define factorial-with-continuation
- (lambda* (n
- #:optional
- ;; Explicitly specifying an argument, which is a
- ;; continuation.
- ;; By default the continuation cont is merely
- ;; returning the value given. Identity
- ;; function. Not doing anything with the result.
- (cont (λ (x) x))
- ;; Using `remainder` only as an example.
- (test (λ (num) (= (remainder num 7) 0))))
- (let iter ([num° (- n 1)]
- [product° n])
- (cond
- ;; Base case of factorial.
- [(<= num° 1) product°]
- ;; Next a case with some condition, which, if true
- ;; means you want to exit.
- [(test num°)
- (cont product°)]
- ;; Otherwise normal iteration.
- [else
- (iter (- num° 1)
- (* product° num°))]))))
- (factorial-with-continuation 10)
- (factorial-with-continuation 10
- ;; A continuation, which merely
- ;; display something, but then
- ;; returns the value.
- (λ (num)
- (display (simple-format #f "~a\n" num))
- num)
- ;; Another weird condition.
- (λ (num) (= (remainder num 17) 0)))
|