Skip to content

Instantly share code, notes, and snippets.

;;; Compare descriptions of two objects and report the number of common features. Descriptions are lists of features
;;; separated by the symbol -VS- (e.g. (large red shiny cube -vs- small shiny red four-sided pyramid)).
(defun right-side (descriptions)
"The features of the object to the right of the separator."
(cdr (member '-vs- descriptions)))
(defun left-side (descriptions)
"The features of the object to the left of the separator."
(right-side (reverse descriptions)))
(defun throw-die ()
"Simulate the throw of a regular die; returns a number between 1 and 6, inclusive."
(1+ (random 6)))
(defun throw-dice ()
"Simulate the throwing of two dice; returns a list."
(list (throw-die) (throw-die)))
(defun snake-eyes-p (throw)
"Is THROW snake-eyes (two ones)?"
@schmalz
schmalz / lfe-tut-20.lfe
Created October 11, 2020 16:45
LFE tutorial 20 - registered process names
(defmodule lfe-tut-20
(export (start 0)
(ping 1)
(pong 0)))
(defun ping
([0]
(! 'pong 'finished)
(lfe_io:format "ping finished~n" ()))
([n]
@schmalz
schmalz / lfe-tut-19.lfe
Created October 10, 2020 16:58
LFe tutorial 19 - message passing
(defmodule lfe-tut-19
(export (start 0)
(ping 2)
(pong 0)))
(defun ping
([0 pong-pid]
(! pong-pid 'finished)
(lfe_io:format "ping finished~n" ()))
([n pong-pid]
@schmalz
schmalz / sqrt.lfe
Created October 9, 2020 13:54
LFE SICP square root first example
(defmodule sqrt
(export (sqrt 1)))
(defun sqrt [x]
(sqrt (* 0.5 x)
x))
(defun sqrt [guess x]
(if (good-enough? guess x)
guess
@schmalz
schmalz / lfe-tut-18.lfe
Last active October 9, 2020 13:52
LFE tutorial 18 - processes
(defmodule lfe-tut-18
(export (start 0)
(say-something 2)))
(defun say-something
([what 0]
'done)
([what times]
(lfe_io:format "~p~n" (list what))
(say-something what (- times 1))))
@schmalz
schmalz / eliza.lisp
Created June 5, 2018 16:15
PAIP: Eliza, Dialog with a Machine - W.I.P.
(defconstant +fail+ nil
"Indicates PAT-MATCH failure.")
(defconstant +no-bindings+ '((t . t))
"Indicates PAT-MATCH success, but with no variables.")
;;; Utilities
(defun starts-with (lst x)
"Is X the first element of LST?"
@schmalz
schmalz / lazy.lisp
Created May 30, 2018 15:18
Land of Lisp: Simple Lazy List Functions
;;;;
(defmacro lazy (&body body)
"Lazily evaluate BODY."
(let ((forced (gensym))
(value (gensym)))
`(let ((,forced nil)
(,value nil))
(lambda ()
(unless ,forced
@schmalz
schmalz / svg.lisp
Created May 25, 2018 08:41
Land of Lisp: SVG Generation
(defun pairs (lst)
(loop for i on lst by #'cddr
collect (cons (car i)
(cadr i))))
(defun print-tag (name attributes closing-p)
""
(princ #\<)
(when closing-p
(princ #\/))
@schmalz
schmalz / dice-of-doom.lisp
Created May 24, 2018 07:48
Land of Lisp: Dice of Doom
(defparameter *num-players* 2)
(defparameter *max-dice* 3)
(defparameter *board-size* 3)
(defparameter *board-hexnum*
(* *board-size* *board-size*))
(defun board-array (lst)
"To allow faster access, create an array containing LST's contents."
(make-array *board-hexnum* :initial-contents lst))