Last active
December 17, 2015 00:29
-
-
Save mrklein/5520999 to your computer and use it in GitHub Desktop.
Two variant of factorial implementation with Bigloo Scheme. Bigloo specifics are in timing of calculations. Tested under Bigloo 4.0a.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(module fact) | |
(define *num-list* '(1 12 123 1234 12345 123456)) | |
(define (factorial n) | |
(define (iter k acc) | |
(if (= k 1) | |
acc | |
(iter (- k 1) (* k acc)))) | |
(iter n 1)) | |
(define (dfactorial n) | |
(do ((r 1) | |
(i 1 (+ i 1))) | |
((= i n) r) | |
(set! r (* r i)))) | |
(define (sum-digits n) | |
(reduce + 0 (map | |
(lambda (c) (- (char->integer c) (char->integer #\0))) | |
(string->list (number->string n))))) | |
;; Taken from example of time function in Bigloo reference | |
(multiple-value-bind (res rtime stime utime) | |
(time | |
(lambda () | |
(reduce + 0 (map sum-digits (map factorial *num-list*))))) | |
(print "Recursive factorial: " (* 0.001 rtime) " sec ")) | |
(multiple-value-bind (res rtime stime utime) | |
(time | |
(lambda () | |
(reduce + 0 (map sum-digits (map dfactorial *num-list*))))) | |
(print "Iterative factorial: " (* 0.001 rtime) " sec ")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment