Skip to content

Commit ed054c8

Browse files
committed
Add Lisp example files
1 parent ba9235d commit ed054c8

File tree

4 files changed

+163
-0
lines changed

4 files changed

+163
-0
lines changed

lisp/test_basics.lisp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
; Arithmetic
2+
(println "Testing arithmetic...")
3+
(def result (+ 1 2 3))
4+
(if (= result 6)
5+
(println " + works: passed")
6+
(println " + works: FAILED"))
7+
8+
(if (= (* 4 5) 20)
9+
(println " * works: passed")
10+
(println " * works: FAILED"))
11+
12+
(if (= (- 10 3) 7)
13+
(println " - works: passed")
14+
(println " - works: FAILED"))
15+
16+
; Comparison
17+
(println "Testing comparison...")
18+
(if (< 1 2)
19+
(println " < works: passed")
20+
(println " < works: FAILED"))
21+
22+
(if (>= 5 5)
23+
(println " >= works: passed")
24+
(println " >= works: FAILED"))
25+
26+
; Functions
27+
(println "Testing functions...")
28+
(def square (fn (n) (* n n)))
29+
(if (= (square 5) 25)
30+
(println " fn works: passed")
31+
(println " fn works: FAILED"))
32+
33+
; Let bindings
34+
(println "Testing let...")
35+
(def let-result (let (x 10 y 20) (+ x y)))
36+
(if (= let-result 30)
37+
(println " let works: passed")
38+
(println " let works: FAILED"))
39+
40+
; Recursion
41+
(println "Testing recursion...")
42+
(def factorial (fn (n)
43+
(if (<= n 1)
44+
1
45+
(* n (factorial (- n 1))))))
46+
(if (= (factorial 5) 120)
47+
(println " recursion works: passed")
48+
(println " recursion works: FAILED"))
49+
50+
(println "")
51+
(println "All basic tests completed!")

lisp/test_macros.lisp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
(println "Testing macros...")
2+
3+
; Defining an 'unless' macro
4+
(defmacro unless (cond body)
5+
(list 'if cond nil body))
6+
7+
(if (= (unless false 42) 42)
8+
(println " unless macro: passed")
9+
(println " unless macro: FAILED"))
10+
11+
; Defining an 'inc' macro
12+
(defmacro inc (x)
13+
(list '+ x 1))
14+
15+
(if (= (inc 5) 6)
16+
(println " inc macro: passed")
17+
(println " inc macro: FAILED"))
18+
19+
; Defining a 'double' macro
20+
(defmacro double (x)
21+
(list '* x 2))
22+
23+
(if (= (double 10) 20)
24+
(println " double macro: passed")
25+
(println " double macro: FAILED"))
26+
27+
; Nested macro expansion
28+
(defmacro inc-twice (x)
29+
(list 'inc (list 'inc x)))
30+
31+
(if (= (inc-twice 5) 7)
32+
(println " nested macros: passed")
33+
(println " nested macros: FAILED"))
34+
35+
(println "")
36+
(println "All macro tests completed!")

lisp/test_pure_functions.lisp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
(println "Testing pure function detection...")
2+
3+
; Defining a pure function
4+
(def square (fn (n) (* n n)))
5+
6+
; This should work (pure function with constant arg could be folded)
7+
(if (= (square 5) 25)
8+
(println " square(5) = 25: passed")
9+
(println " square(5) = 25: FAILED"))
10+
11+
; Nested pure functions
12+
(def double (fn (x) (* x 2)))
13+
(def quad (fn (x) (double (double x))))
14+
15+
(if (= (quad 3) 12)
16+
(println " quad(3) = 12: passed")
17+
(println " quad(3) = 12: FAILED"))
18+
19+
; Pure function with conditional
20+
(def abs (fn (n) (if (< n 0) (- n) n)))
21+
22+
(if (= (abs -5) 5)
23+
(println " abs(-5) = 5: passed")
24+
(println " abs(-5) = 5: FAILED"))
25+
26+
(if (= (abs 5) 5)
27+
(println " abs(5) = 5: passed")
28+
(println " abs(5) = 5: FAILED"))
29+
30+
; Pure function with let
31+
(def sum-squares (fn (a b)
32+
(let (a2 (* a a)
33+
b2 (* b b))
34+
(+ a2 b2))))
35+
36+
(if (= (sum-squares 3 4) 25)
37+
(println " sum-squares(3,4) = 25: passed")
38+
(println " sum-squares(3,4) = 25: FAILED"))
39+
40+
(println "")
41+
(println "All pure function tests completed!")

lisp/test_tco.lisp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
(println "Testing tail call optimization...")
2+
3+
; Sum function with accumulator (tail recursive)
4+
(def sum (fn (n acc)
5+
(if (<= n 0)
6+
acc
7+
(sum (- n 1) (+ acc n)))))
8+
9+
; This would stack overflow without TCO
10+
(def result (sum 10000 0))
11+
(if (= result 50005000)
12+
(println " tail call sum(10000): passed")
13+
(println " tail call sum(10000): FAILED"))
14+
15+
; Even/odd mutual recursion
16+
(def even? (fn (n)
17+
(if (= n 0)
18+
true
19+
(odd? (- n 1)))))
20+
21+
(def odd? (fn (n)
22+
(if (= n 0)
23+
false
24+
(even? (- n 1)))))
25+
26+
(if (even? 100)
27+
(println " mutual recursion even?(100): passed")
28+
(println " mutual recursion even?(100): FAILED"))
29+
30+
(if (odd? 101)
31+
(println " mutual recursion odd?(101): passed")
32+
(println " mutual recursion odd?(101): FAILED"))
33+
34+
(println "")
35+
(println "All TCO tests completed!")

0 commit comments

Comments
 (0)