Lecture overview -- Keyboard shortcut: 'u'  Previous page: Generation of list selectors -- Keyboard shortcut: 'p'  Next page: Mapping and Filtering [Section] -- Keyboard shortcut: 'n'  Lecture notes - all slides together  Annotated slide -- Keyboard shortcut: 't'  Alphabetic index  Help page about these notes  Course home    Recursion and Higher-order Functions - slide 17 : 35

Apply

The function apply calls a function on a list of parameters

> (+ 1 2 3 4)
10

> (+ (list 1 2 3 4))
+: expects argument of type <number>; given (1 2 3 4)

> (apply + (list 1 2 3 4))
10

> (reverse (list 1 2 3 4))
(4 3 2 1)

> (reverse 1 2 3 4)
reverse: expects 1 argument, given 4: 1 2 3 4

> (apply reverse (list 1 2 3 4))
reverse: expects 1 argument, given 4: 1 2 3 4

> (define (reverse-1 . x) (reverse x))

> (reverse-1 1 2 3 4)
(4 3 2 1)

> (apply reverse-1 (list 1 2 3 4))
(4 3 2 1)