Skip to content

Instantly share code, notes, and snippets.

@ehaliewicz
Last active December 14, 2015 10:49
Show Gist options
  • Save ehaliewicz/5074597 to your computer and use it in GitHub Desktop.
Save ehaliewicz/5074597 to your computer and use it in GitHub Desktop.
lisp vm and compiler
;; compile-and-go creates a VM, compiles the given expression,
;; and runs the resulting assembly as the first expression in the vm REPL
(compile-and-go '(define (compiled-tail-recursive-fib n)
(define (fib-iter a b count)
(if (= count 0)
b
(fib-iter (+ a b) a (- count 1))))
(fib-iter 1 0 n)))
;; total-stack-pushes: 0 maximum-stack-depth: 0
;; instructions executed: 11 execution time: 0.0015 seconds
;; => COMPILED-TAIL-RECURSIVE-FIB
GEN-Eval: (define (interpreted-tail-recursive-fib n)
(define (fib-iter a b count)
(if (= count 0)
b
(fib-iter (+ a b) a (- count 1))))
(fib-iter 1 0 n))
;; total-stack-pushes: 3 maximum-stack-depth: 3
;; instructions executed: 55 execution time: 0.05 seconds
;; => INTERPRETED-TAIL-RECURSIVE-FIB
GEN-Eval: (interpreted-tail-recursive-fib 120)
;; total-stack-pushes: 3626 maximum-stack-depth: 10
;; instructions executed: 43066 execution time: 0.0175 seconds
;; => 5358359254990966640871840
GEN-Eval: (compiled-tail-recursive-fib 120)
;; total-stack-pushes: 725 maximum-stack-depth: 3
;; instructions executed: 5988 execution time: 0.0035 seconds
;; => 5358359254990966640871840
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment