Skip to content

Instantly share code, notes, and snippets.

View lispm's full-sized avatar

Rainer Joswig lispm

  • Germany
View GitHub Profile
;;; L-99: Ninety-Nine Lisp Problems
;; Problem 9: Pack consecutive duplicates of list elements into sublists.
; If a list contains repeated elements they should be placed in separate sublists.
;
; Example:
;
; * (pack '(a a a a b c c a a d e e e e))
; ((A A A A) (B) (C C) (A A) (D) (E E E E))
; Original Version from Atabey Kaygun, Conjugate Partitions
; http://kaygun.tumblr.com/post/145269023094/conjugate-partitions
; Derived versions: Rainer Joswig, [email protected], 2016
; version 1 using LOOP
(defun dual (xs &aux k n r)
(loop while xs do
(setf k (reduce #'min xs)
(defun make-vars (vars &aux syms)
(values (loop for var in vars
if (eq var NIL)
collect (let ((sym (gensym "ignore"))) (push sym syms) sym)
else collect var)
syms))
(defmacro multiple-value-bind-some (vars form &body body)
(multiple-value-bind (vars syms) (make-vars vars)
`(multiple-value-bind ,vars ,form
;;; http://xen.garden/wp/?p=25
; Common Lisp version by Rainer Joswig, [email protected], 2016
(defun create-symbol (prefix suffix)
(intern (format nil "~a-~a" prefix suffix)
(symbol-package suffix)))
(defun make-get-set (name
&aux
; Hi and welcome to team Gilded Rose. As you know, we are a small inn
; with a prime location in a prominent city ran by a friendly
; innkeeper named Allison. We also buy and sell only the finest goods.
; Unfortunately, our goods are constantly degrading in quality as they
; approach their sell by date. We have a system in place that updates
; our inventory for us. It was developed by a no-nonsense type named
; Leeroy, who has moved on to new adventures. Your task is to add the
; new feature to our system so that we can begin selling a new
; category of items.
; First an introduction to our system:
(defun diamond (letter)
(labels ((letter-value (letter)
(- (char-code (char-downcase letter))
(char-code #\a)))
(pad-char (char n)
(loop repeat n do (write-char #\space))
(write-char char))
(write-letter-line (letter front back)
(pad-char letter front)
(unless (zerop back)
; 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)
@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
@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 / 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))