Skip to content

Instantly share code, notes, and snippets.

View lispm's full-sized avatar

Rainer Joswig lispm

  • Germany
View GitHub Profile
@lispm
lispm / gist:52d49e61cea60b287074
Last active August 29, 2015 14:13
cross product for two lists
(defun cp (l1 l2)
(mapcan (lambda (e2)
(mapcar (lambda (e1) (list e1 e2)) l1))
l2))
(defun cp (l1 l2)
(iter:iter outer (iter:for i iter:in l1)
(iter:iter (iter:for j iter:in l2)
(iter:in outer (iter:collect (list i j))))))
@lispm
lispm / gist:082860afd06a6209fc76
Created April 9, 2015 11:57
Retrieving Source Code from a running Clozure CL
(defun retrieve-source-code (&optional (package *package*))
(do-symbols (s package)
(multiple-value-bind (symbol where)
(find-symbol (symbol-name s)
package)
(declare (ignore symbol))
(when (member where '(:internal :external))
(let ((ds (find-definition-sources s)))
(when (and ds (listp ds))
(loop for (nil sn) in ds
@lispm
lispm / gist:c143c01c5b52d806249f
Created May 25, 2015 22:16
get the nth prime
(defmethod get-prime ((a integer))
(let ((vector (make-array 100)))
(setf (aref vector 0) 2)
(loop for i from 1 upto a do
(loop for j from (1+ (aref vector (1- i))) do
(when (= i (loop for k below i
until (zerop (mod j (aref vector k)))
finally (return k)))
(setf (aref vector i) j)
(return))))
@lispm
lispm / foo.lisp
Created July 23, 2015 12:10
Pipe, threading, sequencing expressions in Common Lisp
(defmacro pipe (expression &rest expressions)
(if (null expressions)
expression
(destructuring-bind ((fn arg &rest args) &rest more-expressions)
expressions
`(pipe
(let ((,arg ,expression))
(,fn ,arg ,@args))
,@more-expressions))))
; http://ruslanspivak.com/lsbasi-part3/
; Lisp / CLOS version Rainer Joswig, [email protected], 2015
;;; ================================================================
;;; Token
(defclass token ()
((type :accessor token-type :initarg :type)
@lispm
lispm / square-spirals.lisp
Last active August 29, 2015 14:27
Reddit dailyprogrammer [2015-08-10] Challenge #227 [Easy] Square Spirals
; https://www.reddit.com/r/dailyprogrammer/comments/3ggli3/20150810_challenge_227_easy_square_spirals/
; [2015-08-10] Challenge #227 [Easy] Square Spirals
; Rainer Joswig, [email protected], 2015
; portable Common Lisp code
; 'math' version based on Java version by 9speedy
;;; ================================================================
@lispm
lispm / lucky7s.lisp
Created August 29, 2015 23:47
[2015-08-28] Challenge #229 [Hard] Divisible by 7
; https://www.reddit.com/r/dailyprogrammer/comments/3irzsi/20150828_challenge_229_hard_divisible_by_7/
(defun lucky7s (n &aux
(nx0 (floor (+ n 2) 3))
(ny0 (floor (+ n 1) 3))
(nz0 (floor n 3))
(cache (make-hash-table :test 'equal)))
(labels ((nt (k m &aux (n 0) (t0 0) nsub tsub)
(cond ((zerop m)
(values (if (zerop k) 1 0) 0))
@lispm
lispm / split-with.lisp
Created September 12, 2015 00:10
simpler CL split-with implementation than in dash.el
(defun split-with (pred list)
"splits the LIST at the first NIL result of PRED"
(values (loop while (and (not (null list))
(funcall pred (first list)))
collect (pop list))
list))
(defun map-while (predicate function list)
(loop with e
while (not (null list))
@lispm
lispm / take.lisp
Created December 31, 2015 20:45
TAKE in Scheme, CL and Miranda
; page 13, Ralf Hinze, Einführung in die funktionale Programmierung mit Miranda
#|
take' 0 x = []
take' (n+1) [] = []
take' (n+1) (a:x) = a : take' n x
; The ugly Scheme version in the book
(define take
; John McCarthy. Puzzle Solving Program in LISP. Memo 20, Artificial Intelligence Project
; ftp://publications.ai.mit.edu/ai-publications/pdf/AIM-020.pdf
; 1960
; Common Lisp translation: 2016, Rainer Joswig, [email protected]
; basically the code is unchanged, but using s-expression syntax
(defparameter pzl
'((a1 (a2 a5) 7.5)
(a2 (a1 a5 a9 a3) 3.5)