EMZETT.
Login

Kurz: (defun addiere (a b) (+ a b))

Teil des Kurses Common Lisp

Kapitel 2 von 8 im Kurs Common Lisp. Mit Fortschritt, Quiz und Zertifikat auf der Lernseite.

Funktionen definieren

(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))

Ausgabe:

49 7
Hallo, Mia!
Servus, Mia!
15
Tom (18) Zoe (30)
Gibt x zum Quadrat zurück.

Parameter-Marker: &optional (optionale), &rest (beliebig viele), &key (benannte Argumente).

Bedingungen

(defun vorzeichen (n)
  (cond ((> n 0) "positiv")
        ((< n 0) "negativ")
        (t "null")))
 
(defun note (n)
  (case n
    (1 "sehr gut")
    ((2 3) "gut bis befriedigend")
    (t "sonst")))
 
(format t "~a ~a ~a~%" (vorzeichen 5) (vorzeichen -2) (vorzeichen 0))
(format t "~a ~a~%" (note 1) (note 3))
(format t "~a~%" (if (> 3 2) "ja" "nein"))
(when (evenp 4) (format t "gerade~%"))
(unless (evenp 5) (format t "ungerade~%"))
(format t "~a ~a~%" (and 1 2 3) (or nil nil 5))
(format t "~a ~a ~a~%" (null nil) (not t) (zerop 0))

Ausgabe:

positiv negativ null
sehr gut gut bis befriedigend
ja
gerade
ungerade
3 5
T NIL T

In Lisp gilt: nil ist falsch (und die leere Liste), alles andere ist wahr.

Schleifen

(dotimes (i 3) (format t "i=~a " i))
(terpri)
(dolist (x '(a b c)) (format t "~a " x))
(terpri)
 
(let ((i 0))
  (loop
    (when (>= i 3) (return))
    (format t "loop ~a " i)
    (incf i)))
(terpri)
 
(format t "~a~%" (loop for i from 1 to 5 collect (* i i)))
(format t "~a~%" (loop for i from 1 to 10 when (evenp i) collect i))
(format t "~a~%" (loop for x in '(3 1 4 1 5) sum x))
(format t "~a~%" (loop for x in '(3 1 4 1 5) maximize x))
(loop for x in '(a b c) for i from 0 do (format t "~a:~a " i x))
(terpri)
(format t "~a~%" (loop repeat 3 collect 'x))
(do ((n 5 (1- n)) (erg 1 (* erg n))) ((= n 0) (format t "5! = ~a~%" erg)))

Ausgabe:

i=0 i=1 i=2
A B C
loop 0 loop 1 loop 2
(1 4 9 16 25)
(2 4 6 8 10)
14
5
0:A 1:B 2:C
(X X X)
5! = 120

Rekursion

(defun fakultaet (n)
  (if (<= n 1) 1 (* n (fakultaet (- n 1)))))
 
(defun fib (n)
  (labels ((schritt (a b i)
             (if (= i 0) a (schritt b (+ a b) (- i 1)))))
    (schritt 0 1 n)))
 
(defun ggt (a b) (if (zerop b) a (ggt b (mod a b))))
 
(format t "~a~%" (fakultaet 20))
(format t "~a~%" (fakultaet 30))
(format t "~a~%" (fib 50))
(format t "~a~%" (ggt 48 18))

Ausgabe:

2432902008176640000
265252859812191058636308480000000
12586269025
6

Merke

  • defun definiert Funktionen, &optional, &rest, &key regeln die Parameter
  • if, cond, case, when, unless für Verzweigungen; nil ist falsch
  • dotimes, dolist, do und vor allem das mächtige loop für Schleifen
  • Ganzzahlen laufen nie über

Übungsaufgabe

Schreibe potenz rekursiv und mit loop und vergleiche die Ergebnisse.

Quiz zur Selbstkontrolle

Weiter im Kurs

Zurück: Einführung und erstes Programm

Weiter: Listen, Sequenzen und Funktionen höherer Ordnung

Alle Kapitel: Common Lisp im Überblick