Skip to content

Instantly share code, notes, and snippets.

View lispm's full-sized avatar

Rainer Joswig lispm

  • Germany
View GitHub Profile
@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
(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)
; https://stackoverflow.com/a/25264944/69545
; ================================================================
; This is the usual evolution
; 1) simple recursive version
; 2) more efficient tail-recursive version
; 3) efficient loop
;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))
@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)))))
; 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)))
; https://github.com/inconvergent/snek/blob/master/utils/spline-script.lisp
(defun test-centroids (counts nc ncn)
(loop for i from 0 below nc
always (multiple-value-bind (val exists)
(gethash i counts)
(and exists (>= val ncn)))))
@lispm
lispm / earliest EMACS.text
Last active September 11, 2018 18:22
GLS in 2007 about the earliest EMACS development
<GLS is Guy Steele, RMS is Richard Stallman, DLW is Dan Weinreb, Moon is David Moon, Ed is Ed
Schwalenberg, CBF is Charles Frankston, EAK is Earl Killian, ECC is Gene Cicarelli,
RMF is Bob Frankston, and JLK is John Kulp.>
---
I think all of us have been relying on our memories, which can
fail in various ways. Last time around I checked my file folder
of notes about Emacs, which has some useful information, but
not a lot about who did what. Now I have some more information
(defun divisors (n &aux (divisors (list 1)))
(etypecase n
((integer * 1) nil)
((integer 2 *)
(loop with q and r
for i from 2 to (isqrt n)
do (setf (values q r) (truncate n i))
when (zerop r) do (push q divisors) and do (push i divisors))
(if (and (second divisors)
(= (first divisors) (second divisors)))
; John McCarthy. Puzzle Solving Program in LISP. Memo 20, Artificial Intelligence Project
; http://www.bitsavers.org/pdf/mit/ai/aim/AIM-020.pdf
; 1960
; Common Lisp translation: Rainer Joswig, 2016, [email protected]
; basically the code is unchanged, but using s-expression syntax in Common Lisp
(defparameter pzl
'((a1 (a2 a5) 7.5)
(a2 (a1 a5 a9 a3) 3.5)