lumbda/examples/fibonacci.lsp
russell@unturf.com 1326e8a106 Add peephole optimizer, --help, examples, README update
- Peephole optimizer: eliminates VOID+POP pairs, JUMP-to-next-instruction.
  Adjusts jump targets after dead code removal.
- --help/-h and --version/-v flags.
- 4 example programs: fibonacci, generator, mergesort, objects.
- README rewritten: documents bytecode compiler, --fast flag, all features.
- 529 tests green.
2026-04-13 16:56:21 -04:00

21 lines
519 B
Text

;;; fibonacci.lsp — iterative and recursive Fibonacci
;;; Run: python3 uncommonlisp.py --fast examples/fibonacci.lsp
(define (fib-iter n)
(let loop ((a 0) (b 1) (i 0))
(if (= i n) a (loop b (+ a b) (+ i 1)))))
(define (fib-rec n)
(if (<= n 1) n (+ (fib-rec (- n 1)) (fib-rec (- n 2)))))
(display "fib(30) iterative: ")
(display (fib-iter 30))
(newline)
(display "fib(20) recursive: ")
(display (fib-rec 20))
(newline)
(display "First 15 Fibonacci numbers: ")
(display (map fib-iter (iota 15)))
(newline)