This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; https://github.com/inconvergent/snek/blob/master/utils/count.lisp | |
(defun make-counter (&optional ll) | |
(let ((c (make-hash-table :test #'equal))) | |
(loop for a in ll do (counter-add c a)) | |
c)) | |
(defun counter-add (c a) | |
(incf (gethash a c 0))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; the hashtable is found on the binding stack | |
(defmacro memo (v e &aux (§v (gensym "V"))) | |
`(locally (declare (special *mem*)) | |
(let ((*mem* (or (and (boundp '*mem*) *mem*) | |
(make-hash-table))) | |
(,§v ,v)) | |
(declare (special *mem*)) | |
(or (gethash ,§v *mem*) | |
(setf (gethash ,§v *mem*) ,e))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;see https://stackoverflow.com/questions/46496083/lazily-generating-prime-in-common-lisp/46497515#46497515 | |
(defun divisor-in-list (n list) | |
(some (lambda (i) | |
(zerop (rem n i))) | |
list)) | |
(defun primes () | |
(let ((p (list 2)) | |
(n 2)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; https://stackoverflow.com/a/25264944/69545 | |
; ================================================================ | |
; This is the usual evolution | |
; 1) simple recursive version | |
; 2) more efficient tail-recursive version | |
; 3) efficient loop | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun psymb (package &rest args) | |
(values (intern (apply #'mkstr args) package))) | |
(defmacro with-struct ((name . fields) struct &body body) | |
(let ((gs (gensym))) | |
`(let ((,gs ,struct)) | |
(let ,(mapcar #'(lambda (f) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun make (pts &key closed) | |
(let ((n (length pts))) | |
(if (< n 4) (error "must have at least 4 pts.")) | |
(let ((apts (make-dfloat-array n))) | |
(loop | |
for p in pts | |
for i from 0 | |
do | |
(destructuring-bind (x y) | |
p |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; https://github.com/inconvergent/snek | |
(proclaim '(inline last1 single append1 conc1 mklist)) | |
(proclaim '(optimize speed)) | |
(asdf:defsystem "snek" | |
:description "SNEK is Not an Acronym" | |
; :version "0.0.1" | |
:author "Anders Hoff" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun hyphae (sand size fn itt rad mid) | |
(let ((curr (make-hash-table :test #'equal)) | |
(hits 0)) | |
(labels ((init (snk n) | |
(loop for i from 0 below n do | |
(setf (gethash | |
(add-vert! snk (rnd-in-box 250 250 :xy '(250 250))) | |
curr) | |
0))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun sunset (color &key (shift-list '(1 4/3 4/3))) | |
"Redshifts the #RRGGBB color." | |
(format nil | |
"#~{~X~}" | |
(loop for start in '(1 3 5) and shift in shift-list | |
collect (truncate (parse-integer color | |
:start start | |
:end (+ start 2) | |
:radix 16) | |
shift)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define-condition invalid-nucleotide-error (error) | |
((nucleotide :initarg :nucleotide))) | |
(defvar *nuc-table* | |
'((#\G . #\C) (#\C . #\G) (#\T . #\A) (#\A . #\U))) | |
(defun transcribe (nucleotide) | |
(or (cdr (assoc nucleotide *nuc-table*)) | |
(error 'invalid-nucleotide-error :nucleotide nucleotide))) |