Die programmierbare Programmiersprache mit Makros
Ausführen: Datei hallo.lisp speichern und mit sbcl --script hallo.lisp ausführen (SBCL von sbcl.org).
(funktion arg1 arg2); Code ist Datenformat t "...~a~%" gibt Text aus; 'x verhindert die Auswertungdefparameter, defconstant, let, let*, setf für Variablensbcl --script hallo.lisp # Skript ausführen
sbcl # interaktive Konsole (REPL)
* (load "hallo.lisp") ; Datei laden
* (quit) ; beendendefun definiert Funktionen, &optional, &rest, &key regeln die Parameterif, cond, case, when, unless für Verzweigungen; nil ist falschdotimes, dolist, do und vor allem das mächtige loop für Schleifen(defun quadrat (x)
"Gibt x zum Quadrat zurück."
(* x x))
(defun addiere (a b) (+ a b))
(defun begruesse (name &optional (gruss "Hallo"))
(format nil "~a, ~a!" gruss name))
(defun summe (&rest zahlen)
(apply #'+ zahlen))
(defun person (&key name (alter 18))
(format nil "~a (~a)" name alter))
(format t "~a ~a~%" (quadrat 7) (addiere 3 4))
(format t "~a~%" (begruesse "Mia"))
(format t "~a~%" (begruesse "Mia" "Servus"))
(format t "~a~%" (summe 1 2 3 4 5))
(format t "~a ~a~%" (person :name "Tom") (person :alter 30 :name "Zoe"))
(format t "~a~%" (documentation 'quadrat 'function))first/rest, cons, append, nth, remove-if, reduce, sort (destruktiv)mapcar, reduce, funcall, apply, lambda – Funktionen sind Werteformat mit ~{...~} durchläuft Listen(let ((l '(5 3 9 1 7)))
(format t "~a ~a ~a~%" (length l) (first l) (car (last l)))
(format t "~a ~a~%" (nth 2 l) (subseq l 1 3))
(format t "~a~%" (reverse l))
(format t "~a~%" (sort (copy-list l) #'<))
(format t "~a~%" (append l '(8 9)))
(format t "~a ~a~%" (member 9 l) (position 9 l))
(format t "~a~%" (remove 9 l))
(format t "~a~%" (remove-if #'evenp '(1 2 3 4 5 6)))
(format t "~a~%" (remove-duplicates '(1 2 2 3 3 3)))
(format t "~a~%" (push 0 l))
(format t "~a~%" l)
(format t "~a~%" (pop l))
(format t "~a ~a~%" (reduce #'+ l) (reduce #'max l))
(format t "~a~%" (count 1 '(1 2 1 3 1)))
(format t "~a~%" (union '(1 2 3) '(3 4)))
(format t "~a~%" (intersection '(1 2 3) '(2 3 4)))
(format t "~a~%" (set-difference '(1 2 3 4) '(2 4))))'), Backquote (` `), ,x und ,@liste`defmacro erzeugt Code zur Übersetzungszeit; macroexpand-1 zeigt das Ergebnisgensym verhindert Namenskonflikte(let ((code '(+ 1 2 3)))
(format t "~a~%" code)
(format t "~a~%" (first code))
(format t "~a~%" (eval code))
(setf (first code) '*)
(format t "~a = ~a~%" code (eval code)))
(let ((x 10) (name 'y))
(format t "~a~%" `(wert ,x))
(format t "~a~%" `(summe ,@(list 1 2 3) ende))
(format t "~a~%" `(,name ist ,(* x 2))))defstruct erzeugt einfache Datensätze mit Zugriffsfunktionendefclass, defgeneric und defmethod bilden CLOS: Methoden gehören zu generischen Funktionen:before/:after machen Verhalten flexibelhandler-case, handler-bind und restart-case(defstruct person
name
(alter 0)
(hobbys nil))
(let ((p (make-person :name "Mia" :alter 17)))
(format t "~a ~a~%" (person-name p) (person-alter p))
(setf (person-alter p) 18)
(push "Lesen" (person-hobbys p))
(format t "~a ~a~%" (person-alter p) (person-hobbys p))
(format t "~a~%" (person-p p))
(let ((kopie (copy-person p)))
(setf (person-name kopie) "Tom")
(format t "~a ~a~%" (person-name p) (person-name kopie)))
(format t "~a~%" p))format ist eine Mini-Sprache: ~a, ~d, ~f, ~{~}, ~[~]with-open-file schließt Dateien automatisch; read-line liest Zeilenwith-output-to-string)defpackage und in-package schaffen Namensräume(format t "~a und ~s~%" "Text" "Text")
(format t "Dezimal ~d, binär ~b, oktal ~o, hex ~x~%" 255 255 255 255)
(format t "Fließkomma ~f, ~,2f, ~e~%" 3.14159 3.14159 31415.9)
(format t "Breite |~8d|~8@a|~8a|~%" 42 "re" "li")
(format t "~r~%" 21)
(format t "~:r ~@r~%" 3 14)
(format t "~{~a ~}~%" '(1 2 3))
(format t "~{~a~^, ~}~%" '(a b c))
(format t "~{(~a . ~a) ~}~%" '(x 1 y 2))
(format t "~a~[ null~; eins~; zwei~]~%" "Zahl:" 1)
(format t "~:[nein~;ja~]~%" t)
(format t "~(KLEIN~) ~@(titel~)~%")
(format t "~%Mit ~10t Tabulator~%")
(format t "~a~%" (format nil "~3,'0d" 7))
(format t "~,,'.,4:d~%" 1234567)optimize machen Lisp-Code so schnell wie Ccurl -O https://beta.quicklisp.org/quicklisp.lisp
sbcl --load quicklisp.lisp --eval '(quicklisp-quickstart:install)' --eval '(ql:add-to-init-file)'
sbcl --eval '(ql:quickload "alexandria")'(let ((tabelle '(("length" . 3) ("reverse" . 3))))
(dolist (zeile tabelle)
(format t "~10a ~a~%" (car zeile) (cdr zeile))))
(format t "~a~%" (list (type-of 1) (type-of 1/2) (type-of 1.0d0) (type-of #\a)))
(format t "~a~%" (list (equal '(1 2) '(1 2)) (eq 'a 'a) (eql 1.0 1.0) (= 1 1.0)))
(format t "~a~%" (list (string 'abc) (symbol-name 'xyz) (intern "FOO")))
(format t "~a~%" (loop for i below 5 collect (expt 2 i)))