Skip to content

Instantly share code, notes, and snippets.

View lispm's full-sized avatar

Rainer Joswig lispm

  • Germany
View GitHub Profile
; this code iterates over a hashmap
; prints a pair to a string
; collects the strings into a list
; prints the list to a string, by using an format iteration command
; then concatenates the string with two other strings
(defun pr-mal-hash-map (hash-map &optional (print-readably t))
(let ((hash-map-value (types:mal-data-value hash-map)))
(concatenate 'string
"{"
(defun sieve (n &aux (list (loop for i from 2 to n collect i)))
(flet ((fact-p (i n)
(zerop (mod n i))))
(loop for (head . rest) = list then (remove head rest :test #'fact-p)
while head collect head)))
; an iterative prime sieve, which is not limited by stack size
(defun sieve (n &aux (list (loop for i from 2 to n collect i))) ; helper variable LIST of prime candidates
; orig
(defun fl-alpha-cuts(lv alpha)
(let ((i) (n) (fset) (sub_list))
(setq i 0)
(setq n (length lv))
(while (< i n)
(progn
(setq fset (eval (nth i lv)))
;;; Diamond problem
;;; Solutions by Rainer Joswig, [email protected], 2016
;;; See "Diamond Kata"
; A
; B B
; C C
; B B
; A
(defun circular-mapc (function list)
(loop for fast = list then (cddr fast)
for slow = list then (cdr slow)
do
(funcall function (car slow))
(when (eq (cddr fast) (cdr slow))
(return-from circular-mapc nil))))
(defun circular (items)
(check-type items cons)
; https://gigadom.wordpress.com/2012/06/18/working-with-binary-trees-in-lisp/
(defstruct (binary-tree (:conc-name tree-))
"A structure for binary trees.
Creates functions: make-binary-tree, tree-data, tree-left, tree-right, ..."
data left right)
(defun add-item (data tree)
"Add some data to a binary tree. Uses numeric comparisons: =, < and >."
(defun parse-netstring (string
&key (field-separator #\,)
(length/string-separator #\:)
(create-displaced-strings-p nil)
&aux (string-length (length string)))
(when (plusp string-length)
(loop for start = 0 then (+ end-pos 1)
for colon-pos = (position length/string-separator string :start start)
for length = (if colon-pos
(parse-integer string :start start :end colon-pos)
@lispm
lispm / minimize.lisp
Created July 14, 2016 16:24
find the minimum item in a list, using a key to extract a value to minimize
(defun minimize (list &key (pred #'<) (key #'identity))
"returns values: the minimum value and if there was one"
(if (null list)
(values nil nil)
(values (loop with min-e = (first list)
with min-v = (funcall key min-e)
initially (pop list)
for e in list
for v = (funcall key e)
if (funcall pred v min-v) do (setf min-e e min-v v)
(defmacro comp ((e &rest qs) l2)
(if (null qs)
`(cons ,e ,l2)
(let ((q1 (car qs))
(q (cdr qs)))
(if (not (eq (cadr q1) '<-))
`(if ,q1
(comp (,e . ,q) ,l2) ,l2)
(let ((v (car q1))
(l1 (third q1))
(defun locate (x xs)
(let* ((r0 nil)
(r1 nil)
(r2 (substitute-if x
(lambda (a)
(when (< x a)
(setf r0 a r1 t)
t))
xs
:count 1)))