Kurz: > Tipp: sort verändert die Liste (destruktiv). Mit copy-list oder (sort (copy-seq l) …) bleibt das Original erhalten.
Teil des Kurses Common Lisp
Kapitel 3 von 8 im Kurs Common Lisp. Mit Fortschritt, Quiz und Zertifikat auf der Lernseite.
Mit Listen arbeiten
(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))))Ausgabe:
5 5 7
9 (3 9)
(7 1 9 3 5)
(1 3 5 7 9)
(5 3 9 1 7 8 9)
(9 1 7) 2
(5 3 1 7)
(1 3 5)
(1 2 3)
(0 5 3 9 1 7)
(0 5 3 9 1 7)
0
25 9
3
(2 1 3 4)
(3 2)
(3 1)Tipp
sortverändert die Liste (destruktiv). Mitcopy-listoder(sort (copy-seq l) ...)bleibt das Original erhalten.
Funktionen höherer Ordnung
Funktionen sind Werte. #'name verweist auf eine Funktion, lambda erzeugt eine anonyme:
(format t "~a~%" (mapcar #'1+ '(1 2 3)))
(format t "~a~%" (mapcar (lambda (x) (* x x)) '(1 2 3 4)))
(format t "~a~%" (mapcar #'+ '(1 2 3) '(10 20 30)))
(format t "~a~%" (remove-if-not (lambda (x) (> x 2)) '(1 2 3 4)))
(format t "~a~%" (reduce (lambda (a b) (+ (* a 10) b)) '(1 2 3)))
(format t "~a~%" (funcall #'+ 1 2 3))
(format t "~a~%" (apply #'max '(3 9 4)))
(format t "~a~%" (find-if (lambda (x) (> x 3)) '(1 5 2 7)))
(format t "~a~%" (every #'numberp '(1 2 3)))
(format t "~a~%" (some #'stringp '(1 "a" 3)))
(format t "~a~%" (sort '("ccc" "a" "bb") #'< :key #'length))
(defun kompose (f g) (lambda (x) (funcall f (funcall g x))))
(defun addierer (n) (lambda (x) (+ x n)))
(format t "~a~%" (funcall (kompose #'1+ (addierer 10)) 5))
(format t "~a~%" (mapcar (addierer 100) '(1 2)))Ausgabe:
(2 3 4)
(1 4 9 16)
(11 22 33)
(3 4)
123
6
9
5
T
T
(a bb ccc)
16
(101 102)Assoziationslisten, Property Lists und Hashtabellen
(let ((al '((a . 1) (b . 2) (c . 3))))
(format t "~a ~a~%" (cdr (assoc 'b al)) (assoc 'z al)))
(let ((pl '(:name "Mia" :alter 17)))
(format t "~a ~a~%" (getf pl :name) (getf pl :alter)))
(let ((h (make-hash-table :test 'equal)))
(setf (gethash "eins" h) 1)
(setf (gethash "zwei" h) 2)
(format t "~a ~a~%" (gethash "eins" h) (gethash "drei" h))
(format t "~a~%" (hash-table-count h))
(maphash (lambda (k v) (format t "~a=~a " k v)) h)
(terpri)
(remhash "eins" h)
(format t "~a~%" (hash-table-count h))
(multiple-value-bind (wert gefunden) (gethash "zwei" h)
(format t "~a ~a~%" wert gefunden)))Ausgabe:
2 NIL
Mia 17
1 NIL
2
eins=1 zwei=2
1
2 TArrays und Vektoren
(let ((v (vector 5 3 9 1))
(m (make-array '(2 3) :initial-element 0)))
(setf (aref v 0) 50)
(format t "~a ~a~%" v (length v))
(setf (aref m 1 2) 7)
(format t "~a~%" m)
(format t "~a~%" (sort v #'<))
(format t "~a~%" (map 'vector #'1+ v))
(let ((dyn (make-array 0 :adjustable t :fill-pointer 0)))
(vector-push-extend 1 dyn)
(vector-push-extend 2 dyn)
(format t "~a ~a~%" dyn (length dyn))))Ausgabe:
#(50 3 9 1) 4
#2A((0 0 0) (0 0 7))
#(1 3 9 50)
#(2 4 10 51)
#(1 2) 2Strings
(let ((s "Hallo Lisp"))
(format t "~a ~a ~a~%" (length s) (string-upcase s) (string-downcase s))
(format t "~a ~a~%" (char s 0) (subseq s 6))
(format t "~a~%" (search "Lisp" s))
(format t "~a~%" (concatenate 'string s "!" "!"))
(format t "~a~%" (string-trim " " " x "))
(format t "~a ~a~%" (string= "a" "a") (string< "a" "b"))
(format t "~a~%" (reverse s))
(format t "~a~%" (substitute #\a #\o "Foo"))
(format t "~a ~a~%" (parse-integer "42") (write-to-string 3.5))
(format t "~a~%" (coerce "abc" 'list))
(format t "~{~a~^, ~}~%" '(1 2 3))
(format t "~5d|~8,2f|~a~%" 42 3.14159 (char-code #\A)))Ausgabe:
10 HALLO LISP hallo lisp
H Lisp
6
Hallo Lisp!!
x
T 0
psiL ollaH
Faa
42 3.5
(a b c)
1, 2, 3
42| 3.14|65Merke
- Listen:
first/rest,cons,append,nth,remove-if,reduce,sort(destruktiv) mapcar,reduce,funcall,apply,lambda– Funktionen sind Werte- Datenstrukturen: Assoziationslisten, Property Lists, Hashtabellen, Vektoren
formatmit~{...~}durchläuft Listen
Übungsaufgabe
Zähle mit einer Hashtabelle die Wörter eines Satzes.
Quiz zur Selbstkontrolle
Was macht mapcar?
- Wendet eine Funktion auf jedes Element an (richtig)
- Zeichnet eine Karte
- Sortiert
- Löscht Elemente
Warum sollte man vor sort copy-list nutzen?
- sort verändert die Liste destruktiv (richtig)
- sort ist langsam
- Nur wegen der Optik
- sort ist verboten
Was erzeugt (lambda (x) ...)?
- Eine anonyme Funktion (richtig)
- Eine Variable
- Eine Liste
- Eine Klasse
Weiter im Kurs
Zurück: Funktionen und Kontrollfluss
Weiter: Makros und Code als Daten
Alle Kapitel: Common Lisp im Überblick