Skip to content

Instantly share code, notes, and snippets.

View lispm's full-sized avatar

Rainer Joswig lispm

  • Germany
View GitHub Profile
(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)
; 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:
;;; 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
(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
; 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)
;;; 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))
(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)))
(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))
@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)
(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)