# Vorlesung 04 – Funktionen, Lambda, Reduktion

## Argument-listen

### &optional

```(defun add-n-or-1 (number &optional (adder 1)) ``````     (+ number adder) ````   )`

```(add-n-or-1 3) => 4```

```(add-n-or-1 5 6) => 11```

### &rest

```(defun add-n-or-more (number &rest arg) ``````      (+ number (reduce #'* arg)) ````   )`

`(add-n-or-more 1 1 2 3 4 5)`

`=> 121`

### &key

```(defun add-or-subtract (number &key (add 0) (sub 0) ``````     (+ number (- sub) add) ````   )`

`(add-or-subtract 12 :sub 5 :add 10)`

=> 17

### &aux

```(defun add-random (number &aux num) ``````     (setf num (random 12)) ``````     (+ number num) ````    )`

## Lokale Funktionen

### flet

`(flet ((mydouble (x) (* x 2)))`
`     (mydouble 4)`
`   )`

=> 8

### labels

```(labels ((mydouble (x) (* x 2)) ``````         (mylistdouble (&rest list) ``````              (mapcar #'mydouble list))) ``````    (mylistdouble 1 2 3 4 5) ````  )`

=> (2 4 6 8 10)

## Lambda

```(lambda (x y) ``````    (+ x y) ````   )`

`=> #<anonymous interpreted function 2008445A>`

`(mapcar #'(lambda (x) (* x 2))`
`    '(1 2 3 4))`

`=> (2 4 6 8)`

`(mapcar #'(lambda (x y) (* x y))`
`'(1 2 3 4) '(1 2 3 4))`

`=> (1 4 9 16)`

`(defun list-square (list)`
`      (mapcar #'(lambda (x y) (* x y))`
`        list list)`
`    )`

`=> list-square`

`(list-square '(2 4 6 8 10))`

`=>  (4 16 36 64 100)`