Skip to content

Instantly share code, notes, and snippets.

View lispm's full-sized avatar

Rainer Joswig lispm

  • Germany
View GitHub Profile
; 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)))
@lispm
lispm / memo-fib.lisp
Created October 13, 2017 10:35
Memoizing Fibonacci in Common Lisp
; 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)))))
;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))
; https://stackoverflow.com/a/25264944/69545
; ================================================================
; This is the usual evolution
; 1) simple recursive version
; 2) more efficient tail-recursive version
; 3) efficient loop
(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)
@lispm
lispm / make.lisp
Last active February 4, 2019 09:31
(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
; 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"
(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)))
(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))))
(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)))