Newsflash: first use-case of let-values in the history of scheme ;-) pasted by mejja on Tue Jun 21 03:43:15 2016
;;; mejja plays with let-values and dijkstras fib: ;;; www.cs.utexas.edu/users/EWD/ewd06xx/EWD654.PDF (define (fib n) ;fantastic? (if (< 0 n) (let-values (((a b) (fib (quotient n 2)))) (let ((c (* (- (* 2 b) a) a)) (d (+ (* b b) (* a a)))) (if (= 0 (remainder n 2)) (values c d) (values d (+ c d))))) (values 0 1))) (define (fib n) ;using let*-values ;; assumption: (div n d) => (values q r) (if (< 0 n) (let*-values (((q r) (div n 2)) ((a b) (fib q))) (let ((c (* a (- (* 2 b) a))) (d (+ (* a a) (* b b)))) (if (= r 0) (values c d) (values d (+ c d))))) (values 0 1)))
standard cps fibs pasted by mejja on Tue Jun 21 03:52:30 2016
(define (fib n)
(define (fib n k)
(if (< 0 n)
(fib (quotient n 2)
(lambda (a b)
(let ((c (* a (- (* 2 b) a)))
(d (+ (* a a) (* b b))))
(if (= 0 (remainder n 2))
(k c d)
(k d (+ c d))))))
(k 0 1)))
(guarantee-exact-nonnegative-integer n 'FIB)
(fib n cons))
portable version for lesser schemes.. added by mejja on Tue Jun 21 04:10:12 2016
(define (fib n #!optional cont)
(define (fib n k)
(if (< 0 n)
(fib (quotient n 2)
(lambda (a b)
(let ((c (* a (- (* 2 b) a)))
(d (+ (* a a) (* b b))))
(if (= 0 (remainder n 2))
(k c d)
(k d (+ c d))))))
(k 0 1)))
(if (and (exact? n) (not (negative? n)) (integer? n))
(fib n (if (default-object? cont) cons cont))
(error ":not-exact-nonnegative-integer" n 'FIB)))