Was ist Common Lisp?
Lisp (List Processing, 1958, John McCarthy) ist die zweitälteste noch verbreitete Programmiersprache. Common Lisp (ANSI-Standard 1994) vereint die Dialekte zu einer großen, stabilen Sprache mit S-Ausdrücken: Code und Daten haben dieselbe Form – verschachtelte Listen in Klammern. Dadurch kann Lisp Code als Daten behandeln und mit Makros die Sprache selbst erweitern.
Common Lisp bietet dynamische Typisierung (optional mit Typdeklarationen), automatische Speicherverwaltung, ein Objektsystem (CLOS), ein Condition-System für Fehler und einen Compiler zu schnellem Maschinencode. Eingesetzt wurde Lisp in der KI-Forschung, in Emacs (Emacs Lisp), bei Google Flights (ITA) und in Grammarly.
Dieser Kurs nutzt SBCL (Steel Bank Common Lisp).
Ausführen
sbcl --script hallo.lisp # Skript ausführen
sbcl # interaktive Konsole (REPL)
* (load "hallo.lisp") ; Datei laden
* (quit) ; beendenFür den Alltag nutzt man Quicklisp (Paketmanager) und SLIME oder Sly (Emacs) bzw. Alive (VS Code).
Das erste Programm
(format t "Hallo, Welt!~%")
(format t "1 + 2 = ~a~%" (+ 1 2))
(print "mit print")
(terpri)
(princ "ohne Anführungszeichen")
(terpri)Hallo, Welt! 1 + 2 = 3 "mit print" ohne Anführungszeichen
Jeder Aufruf ist eine Liste: (funktion argument1 argument2 ...) – Präfixnotation. ~a in format setzt einen Wert ein, ~% ist ein Zeilenumbruch, t steht für die Standardausgabe.
Rechnen mit Präfixnotation
(format t "~a ~a ~a ~a~%" (+ 1 2 3) (- 10 3) (* 2 3 4) (/ 10 4))
(format t "~a ~a ~a~%" (floor 7 2) (mod 7 3) (rem -7 3))
(format t "~a ~a ~a~%" (expt 2 10) (sqrt 16) (abs -5))
(format t "~a ~a~%" (max 3 9 4) (min 3 9 4))
(format t "~a ~a ~a~%" (float 1/3) (round 2.5) (round 3.5))
(format t "~a~%" (expt 2 100))
(format t "~a ~a~%" (* 1.0 1/3) (/ 6 4))
(format t "~,3f ~a~%" pi (truncate 3.99))
(format t "~a ~a ~a~%" (< 1 2 3) (= 1 1) (equal "a" "a"))6 7 24 5/2 3 1 -1 1024 4.0 5 9 3 0.33333334 2 4 1267650600228229401496703205376 0.33333334 3/2 3.142 3 T T T
Lisp hat einen numerischen Turm: Ganzzahlen beliebiger Größe, Brüche (1/3), Fließkommazahlen und komplexe Zahlen.
Variablen und Konstanten
(defparameter *gruss* "Hallo") ; globale, veränderbare Variable (Konvention: *sterne*)
(defconstant +maximum+ 100) ; Konstante (+plus+)
(let ((x 10) (y 20)) ; lokale Variablen
(format t "~a~%" (+ x y)))
(let* ((a 2) (b (* a 3))) ; let* erlaubt Bezug auf frühere Bindungen
(format t "~a ~a~%" a b))
(setf *gruss* "Servus")
(format t "~a ~a~%" *gruss* +maximum+)
(let ((zaehler 0))
(incf zaehler 5)
(decf zaehler 2)
(format t "~a~%" zaehler))30 2 6 Servus 100 3
Listen und Symbole
(format t "~a~%" '(1 2 3)) ; ' verhindert die Auswertung
(format t "~a~%" (list 1 (+ 1 1) 3))
(format t "~a ~a~%" (car '(a b c)) (cdr '(a b c)))
(format t "~a ~a~%" (first '(a b c)) (rest '(a b c)))
(format t "~a~%" (cons 'x '(y z)))
(format t "~a ~a~%" (symbolp 'abc) (listp '(1)))
(format t "~a~%" (quote (1 + 2)))
(format t "~a~%" (eval '(+ 1 2)))(1 2 3) (1 2 3) A (B C) A (B C) (X Y Z) T T (1 + 2) 3
So führst du den Code aus: Datei hallo.lisp speichern und mit sbcl --script hallo.lisp ausführen (SBCL von sbcl.org).
Merke
- Alles ist ein S-Ausdruck:
(funktion arg1 arg2); Code ist Daten format t "...~a~%"gibt Text aus;'xverhindert die Auswertungdefparameter,defconstant,let,let*,setffür Variablen- Common Lisp rechnet mit Ganzzahlen beliebiger Größe und mit Brüchen
Aufgabe
Berechne in Lisp die Fläche eines Kreises mit Radius 3 und gib sie mit zwei Nachkommastellen aus.