-
Notifications
You must be signed in to change notification settings - Fork 0
/
fibonacci.lisp
41 lines (35 loc) · 1.38 KB
/
fibonacci.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
;;; fibonacci.lisp - Calculate the first 25 fibonacci numbers.
;;
;; This is a sample input file for our minimal lisp interpreter.
;;
;; We use it to demonstrate and test some basic features.
;;
;; Here we use "while" from our standard library, and have defined a
;; function to turn "1" into "1st", etc, as appropriate. This uses our
;; "match" primitive, which is implemented in golang.
;;
;; Add a suitable suffix to a number.
;;
;; e.g. 1 -> 1st
;; 11 -> 11th
;; 21 -> 21st
;; 333 -> 333rd
(set! add-numeric-suffix (fn* (n)
"Add a trailing suffix to make a number readable."
(cond
(match "(^|[^1]+)1$" n) (sprintf "%dst" n)
(match "(^|[^1]+)2$" n) (sprintf "%dnd" n)
(match "(^|[^1]+)3$" n) (sprintf "%drd" n)
true (sprintf "%dth" n)
)))
;; Fibonacci function
(set! fibonacci (fn* (n)
"Calculate the Nth fibonacci number."
(if (<= n 1)
n
(+ (fibonacci (- n 1)) (fibonacci (- n 2))))))
;; Now call our function in a loop, twenty times.
(let* (n 1)
(while (<= n 25)
(print "%s fibonacci number is %d" (add-numeric-suffix n) (fibonacci n))
(set! n (+ n 1) true)))