Skip to content

Instantly share code, notes, and snippets.

View daniel-cussen's full-sized avatar

Daniel Cussen daniel-cussen

  • Bay Area
View GitHub Profile
@daniel-cussen
daniel-cussen / d7entropy.lisp
Created May 21, 2012 22:40
Roll the dice four or five times and get yourself a new random-state
(defun converter (d20 d12 d10 d8 d4)
(let ((prod (+ (* 3840 (1- d20))
(* 320 (1- d12))
(* 32 (1- d10))
(* 4 (1- d8))
(1- d4))))
(if (> prod 65535)
nil
(format nil "~4,'0X" prod))))
@daniel-cussen
daniel-cussen / gist:858859
Created March 7, 2011 17:43
#'apply and #'append
> (apply #'append '((1 2) (3 4) (5 6)))
(1 2 3 4 5 6)
(defun flatten (x)
(labels ((rec (x acc)
(cond ((null x) acc)
((atom x) (cons x acc))
((null (car x)) (cons nil (rec (cdr x) acc)))
(t (rec (car x) (rec (cdr x) acc))))))
(rec x nil)))
@daniel-cussen
daniel-cussen / gist:850116
Created March 1, 2011 23:24
Macro-caller function
(defun macro-caller (x y)
(eval `(macro-in-question ,x ,y)))
@daniel-cussen
daniel-cussen / gist:841330
Created February 23, 2011 22:23
Another example
CL-USER> (repl)
> ((label my-append (lambda (x y) (cond (eq '() x) y
't (cons (car x) (my-append (cdr x) y)))))
(cons '1 (cons '2 '()))
(cons '3 (cons '4 '())))
(1 . (2 . (3 . (4 . NIL))))
;Lisp in Lisp in Golomb forests. Includes use of linked lists.
;Based on Paul Graham's version of McCarthy's paper and code.
;Uses only eq, cond, atom, quote, cons, car, and cdr.
(defun null. (x)
(eq x '()))
(defun and. (x y)
(cond (x
(cond (y 't)
(defun golomb-forest (&rest args)
(if (and (oddp (length args)) (null (car (last args))))
(sentinel-change. (golomb-forest-1 0 args))
(golomb-forest-1 0 args)))
(defun golomb-forest-1 (size lst)
(cond ((null lst) nil)
((= size 0) (cons (car lst) (golomb-forest-1 1 (cdr lst))))
(t (let ((siz (expt 2 size)))
(if (> siz (length lst))
@daniel-cussen
daniel-cussen / gist:840841
Created February 23, 2011 18:03
Starting the golomb forest lisp repl with the optional memory argument
CL-USER> (repl '((a . 1) (b . 2) (c . 3)))
> (cons a (cons b c))
(1 . (2 . 3))
@daniel-cussen
daniel-cussen / gist:840835
Created February 23, 2011 18:01
Behavior of quote in golomb forest lisp repl
> (cdr '(1 2 3))
((2 . 3) . NIL)
@daniel-cussen
daniel-cussen / gist:840832
Created February 23, 2011 17:59
Syntax for #'label
((label conser (lambda (x) (cons x x))) '1)