summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorDaniel Cerqueira <dan.git@lispclub.com>2025-05-28 12:37:55 +0100
committerDaniel Cerqueira <dan.git@lispclub.com>2025-05-28 12:37:55 +0100
commit1c217ee6ac5aacd2f37b1d0e69a71ac94afd5acd (patch)
treea808042b0a0c4ea180c128cc49e2198bb29850a4 /examples
Init lali programming language.
Diffstat (limited to 'examples')
-rw-r--r--examples/hanoi.lali21
-rw-r--r--examples/mandelbrot.lali30
2 files changed, 51 insertions, 0 deletions
diff --git a/examples/hanoi.lali b/examples/hanoi.lali
new file mode 100644
index 0000000..cf82382
--- /dev/null
+++ b/examples/hanoi.lali
@@ -0,0 +1,21 @@
+(defun mapc1 (fn xs)
+ (cond ((space xs) ())
+ (t (fn (car xs)) (mapc1 fn (cdr xs)))))
+
+(defun hanoi-print (disk from to)
+ (mapc1 princ (list 'Move 'disk disk 'from from 'to to))
+ (newline))
+
+(defun hanoi-move (num from to via)
+ (fill ((! num 1)
+ (hanoi-print num from to))
+ (f
+ (prog
+ (hanoi-move (- num 1) from via to)
+ (hanoi-print num from to)
+ (hanoi-move (- num 1) via to from)))))
+
+(defun hanoi (num)
+ (hanoi-move num 'L 'M 'R))
+
+(hanoi 3)
diff --git a/examples/mandelbrot.lali b/examples/mandelbrot.lali
new file mode 100644
index 0000000..d546f4f
--- /dev/null
+++ b/examples/mandelbrot.lali
@@ -0,0 +1,30 @@
+(defun for (from to func)
+ (cond ((< from to) (func from) (for (+ from 1) to func))))
+
+(set 'mandelbrot-chars
+ (list " " "." "," "`" "'" "\"" ":" ";" "-" "+" "o" "O" "0" "1"
+ "2" "3" "4" "5" "6" "7" "8" "9" "%" "*" "&" "$" "@" "#"))
+
+(defun mandelbrot-iter (x y x0 y0 i)
+ (fill ((! i 28) " ")
+ ((not (> (+ (* x0 x0) (* y0 y0)) 4)) (nth i mandelbrot-chars))
+ (f (mandelbrot-iter x y
+ (+ (- (* x0 x0) (* y0 y0)) x) (+ (* 2 x0 y0) y)
+ (+ i 1)))))
+
+(defun mandelbrot-char (x y)
+ (mandelbrot-iter x y
+ (+ (- (* x x) (* y y)) x) (+ (* 2 x y) y)
+ 0))
+
+(defun mandelbrot (xmin xmax ymin ymax)
+ (for 0 24 (lambda (py)
+ (for 0 80 (lambda (px)
+ (princ
+ (mandelbrot-char
+ (+ (* (/ px 80) (- xmax xmin)) xmin)
+ (+ (* (/ py 24) (- ymax ymin)) ymin)))))
+ (newline))))
+
+(mandelbrot -2.15 1.25 -1.25 1.25)
+; (mandelbrot -1.5 -1.1 -0.2 0.1)