Lisp Cheat Sheet
Page content
Lisp Cheat Sheet
* (car '(A B (C)))
A
* (cdr '(A (C)))
((C))
#‘fun ist eine Abkürzung von (function fun)
Reihenfolge: (- 5 2) -> 3 (>= 5 2) -> T
(defmacro setfun (symb fun)
`(prog1 ',symb (setf (symbol-function ',symb) ,fun)))
;; Makro um einen Lambda-Ausdruck als Funktion an ein Symbol zu binden.
(apply #'+ 1 2 3 '(4 5 6)) ;; => 21
(let (a b (c 3) (d (+ 1 2))) (list a b c d)) ;; (NIL NIL 3 3)
;;LET is special form for variable binding. Bindings are described in two element lists where the first element specifies name and the second is code to compute its value, or single variable without default initialization. There are also declarations possible before body.
Keyword Parameter
(defun show-members (&key a b c d ) (write (list a b c d)))
(show-members :a 1 :c 2 :d 3)
(1 NIL 2 3)
REPL - Read Eval Print Loop
(defun repl()
(loop (print (eval (read)))))
;;WIKI:
;;A read–eval–print loop (REPL), also termed an interactive toplevel or language shell, is a simple interactive
;;computer programming environment that takes single user inputs, executes them, and returns the result to the user;
;;a program written in a REPL environment is executed piecewise.[1] The term usually refers to programming interfaces
;;similar to the classic Lisp machine interactive environment. Common examples include command-line shells and similar
;;environments for programming languages, and the technique is very characteristic of scripting languages.
Liste sortieren
(set 'werte '(2 1 6 5 3 4))
(2 1 6 5 3 4)
* (sort werte #'<)
(1 2 3 4 5 6)
;;gefährlich, da die Liste verändert wird
;; Abhilfe
(sort (copy-list werte) #'<)
; eine weitere Liste für ein paar Tests
(defvar *friends* '(((:name . "Peter") (:age . 32))
((:name . "Eva") (:age . 29))
((:name . "Maria") (:age . 30))))
;; Sortierkriterium
(sort (copy-list *friends*)
(lambda (a b)
(<= (cdr (assoc :age a))
(cdr (assoc :age b)))))
;; alternativer Aufruf
(sort (copy-list *friends*) #'<=
:key (lambda (el) (cdr (assoc :age el))))
Listen erzeugen
;; rekursive Variante
(defun n-elemente (n el)
(if (<= n 0) nil
;; else
(cons el (n-elemente (- n 1) el))))
;; endrekursive Variante (tail recursion)
(defun n-elemente (n el &optional acc)
(if (<= n 0) acc
(n-elemente (- n 1) el (cons el acc))))
;; iterative Variante
(defun n-elemente (n el)
(let ((resultat nil))
(dotimes (i n resultat)
(setf resultat (cons el resultat)))))
(n-elemente 3 kurse)
(setq liste (n-elemente 500000 kurse) dummy :fertig)
(nth 325000 liste)
unterschied defun setfun
(defun incr (n) (+ n 1))
(setfun incr (lambda (n) (+ n 1))
Currying in Lisp
(defun curry2r (f)
(lambda (b)
(lambda (a)
(funcall f a b))))
(defun checker (&rest validators)
(lambda (elem)
(every (lambda (check) (funcall check elem)) validators)))
(setfun gt (curry2r #'>))
(setfun lt (curry2r #'<))
(setfun in-range (checker (gt 5) (lt 15)))
(in-range 10)
;; ### Allgemeine Variante ###
;;ist streng genommen nicht currying, da auch mehr als 1 argument übergeben werden kann
(defun curry-n (f numargs)
(lambda (&rest args)
(if (>= (length args) numargs)
(apply f args)
(curry-n
(lambda (&rest restargs) (apply f (append args restargs)))
(- numargs (length args))))))