diff --git a/contents/IFS/IFS.md b/contents/IFS/IFS.md index d16a44efc..2ed1fe484 100644 --- a/contents/IFS/IFS.md +++ b/contents/IFS/IFS.md @@ -140,6 +140,8 @@ Here, instead of tracking children of children, we track a single individual tha [import:5-12, lang:"python"](code/python/IFS.py) {% sample lang="c" %} [import:18-29, lang:"c"](code/c/IFS.c) +{% sample lang="lisp" %} +[import:5-14, lang:"lisp"](code/clisp/ifs.lisp) {% sample lang="coco" %} [import:4-16, lang:"coconut"](code/coconut/IFS.coco) {% sample lang="java" %} @@ -224,6 +226,8 @@ In addition, we have written the chaos game code to take in a set of points so t [import, lang:"python"](code/python/IFS.py) {% sample lang="c" %} [import, lang:"c"](code/c/IFS.c) +{% sample lang="lisp" %} +[import, lang:"lisp"](code/clisp/ifs.lisp) {%sample lang="coco" %} [import, lang:"coconut"](code/coconut/IFS.coco) {%sample lang="java" %} diff --git a/contents/IFS/code/clisp/ifs.lisp b/contents/IFS/code/clisp/ifs.lisp new file mode 100644 index 000000000..4354ab54b --- /dev/null +++ b/contents/IFS/code/clisp/ifs.lisp @@ -0,0 +1,28 @@ +;;;; Iterated Function System implementation + +(defstruct (point (:constructor make-point (x y))) x y) + +(defun chaos-game (iterations shape-points) + "Plays a chaos game with a certain shape for a determined amount of iterations" + (loop + repeat iterations + for rand-point = (svref shape-points (random (length shape-points))) + for point = (make-point (random 1.0) (random 1.0)) ; starting point + then (make-point + (* 0.5 (+ (point-x rand-point) (point-x point))) + (* 0.5 (+ (point-y rand-point) (point-y point)))) ; every subsequent point + collect point)) + +(defparameter *shape-points* + (map + 'vector + (lambda (e) (apply #'make-point e)) + ;; the backquote allows us to selectively evaluate (sqrt 0.75) with the comma + `((0 0) (0.5 ,(sqrt 0.75)) (1 0)))) + +;; output the data to the "out.dat" file +(with-open-file (out "out.dat" :direction :output :if-exists :supersede) + (flet ((format-point (p) + ;; this is not very clean, but it's the simplest way to insert a tab into a string. + (format nil "~f~c~f" (point-x p) #\tab (point-y p)))) + (format out "~{~a~%~}" (map 'list #'format-point (chaos-game 10000 *shape-points*)))))