proper lexical scoping now woo

This commit is contained in:
Lobo 2026-01-14 12:10:29 -03:00
parent ce9489b5d2
commit 27df5f8ce0
13 changed files with 349 additions and 45 deletions

22
examples/fizzbuzz.scm Normal file
View file

@ -0,0 +1,22 @@
(def each-integer-aux
(fn (n i thunk)
(if (= n 0)
'()
(progn
(thunk (- (+ i 1) n))
(each-integer-aux (- n 1) i thunk)))))
(def each-integer
(fn (n thunk)
(each-integer-aux n n thunk)))
(each-integer 30
(fn (x)
(if (or (= 0 (% x 3)) (= 0 (% x 5)))
(progn
(if (= 0 (% x 3))
(write "Fizz"))
(if (= 0 (% x 5))
(write "Buzz")))
(print x))
(write "\n")))

19
examples/map.scm Normal file
View file

@ -0,0 +1,19 @@
(def defn
(mac (name args . body)
(list 'def name (cons 'fn (cons args body)))))
(defn map-aux (f acc l)
(if (nil? l)
(acc f '())
(map-aux
f
(fn (f ys)
(acc f (cons (f (head l)) ys)))
(tail l))))
(defn map (f l)
(map-aux f (fn (f x) x) l))
(println
(map (fn (x) (* x x))
'(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20)))

17
examples/tailcalls.scm Normal file
View file

@ -0,0 +1,17 @@
(def fib-iter (fn (n a b)
(if (= n 0)
a
(fib-iter (- n 1) b (+ a b)))))
(def fib (fn (n) (fib-iter n 0 1)))
(write "(fib 50) = ")
(println (fib 50))
(def sum
(fn (n acc)
(if (= n 0)
acc
(sum (- n 1) (+ n acc)))))
(write "(sum 1000000) = ")
(println (sum 1000000 0))