-
functions
- functions and parameters
- function data & lambda
- Floating Topic
-
three basic components
- functions
-
variables
- 3/4 names are functions
- object system is built upon a extension to functions
-
macros
- macros are also functions, that are used to generate code
-
defun
-
(defun name (parameter*)
"Optional documentation string."
body-form*)
- (documentation 'fun-name 'function)
-
parameter list
- required parameters
- optional parameters
-
rest parameters
- a variable number of arguments
- key word parameters
-
mixing different parameter types
- first the names of the required parameters
- second the optional parameters
- then the rest parameter
- finally the keyword parameter
- (defun hello ())
- (defun hello () () )
-
optional parameters
-
(defun foo (a b &optional c d) (list a b c d) )
- (foo 1 2) ==> (1 2 NIL NIL)
- (foo 1 2 3) ==> (1 2 3 NIL)
- (foo 1 2 3 4) ==> (1 2 3 4)
-
(defun foo (a &optional (b 10)) (list a b))
- (foo 1 2) ==> (1 2)
- (foo 1) ==> (1 10)
-
(defun foo (a b &optional (c 3 c-supplied-p))
(list a b c c-supplied-p))
- (foo 1 2) ==> (1 2 3 NIL)
- (foo 1 2 3) ==> (1 2 3 T)
- (foo 1 2 4) ==> (1 2 4 T)
-
rest parameters
-
(defun hello(&rest all) (print all) )
- (hello 1) => (1)
- (hello 1 2) => (1 2)
- (defun format (stream string &rest values) ...)
- (defun + (&rest numbers) ...)
-
Keyword Parameters
-
(defun foo (&key a b c) (list a b c))
- (foo) ==> (NIL NIL NIL)
- (foo :a 1) ==> (1 NIL NIL)
- (foo :b 1) ==> (NIL 1 NIL)
- (foo :c 1) ==> (NIL NIL 1)
- (foo :a 1 :c 3) ==> (1 NIL 3)
- (foo :a 1 :b 2 :c 3) ==> (1 2 3)
- (foo :a 1 :c 3 :b 2) ==> (1 2 3)
-
(defun foo (&key (a 0) (b 0 b-supplied-p) (c (+ a b)))
(list a b c b-supplied-p))
- (foo :a 1) ==> (1 0 1 NIL)
- (foo :b 1) ==> (0 1 1 T)
- (foo :b 1 :c 4) ==> (0 1 4 T)
- (foo :a 2 :b 1 :c 4) ==> (2 1 4 T)
-
Mixing Different Parameter Types
- sequence. see /* section defun */
-
valid combinations
- required parameters with one other flavor
- &optional and &rest parameters
-
invalid combinations
-
&optional with &key
(defun foo (x &optional y &key z) (list x y z))
- (foo 1 2 :z 3) ==> (1 2 3)
- (foo 1) ==> (1 nil nil)
- (foo 1 :z 3) ==> ERROR
-
&rest with &key
(defun foo (&rest rest &key a b c) (list rest a b c))
- (foo :a 1 :b 2 :c 3) ==> ((:A 1 :B 2 :C 3) 1 2 3)
-
Function Return Values. RETURN-FROM
- Function default returned value,
The value of the last expression evaluated
-
Special operation return-from
- return-from is used to return from a block of code
defined with the BLOCK special operator
- TURN-FROM is a special operator whose
first "argument" is the name of the block
from which to return.
This name isn't evaluated and thus isn't quoted.
- defun automatically wraps the whole function body
in a block with the same name as the function
- evaluating a RETURN-FROM with the
name of the function and
the value you want to return
will cause the function to immediately exit
with that value.
- having to specify the name of the function you're returning from is a bit of a pain
- (defun foo (n)
(dotimes (i 10)
(dotimes (j 10)
(when (> (* i j) n)
(return-from foo (list i j))))))
-
Functions As Data
-
(defun foo (x) (* 2 x))
- CL-USER> (function foo)
#<Interpreted Function FOO>
- CL-USER> #'foo
#<Interpreted Function FOO>
- The syntax #' is syntactic sugar for FUNCTION
- just the way ' is syntactic sugar for QUOTE
-
funcall
- (foo 1 2 3) === (funcall #'foo 1 2 3)
- (defun plot (fn min max step)
(loop for i from min to max by step do
(loop repeat (funcall fn i) do (format t "*"))
(format t "~%")))
-
apply
- (apply #'plot plot-data-list)
- also accept "loose" arguments as long as the last argument is a list
- (apply #'plot #'exp (pop plot-data-list))
-
lambda
-
(lambda (parameters) body)
- (funcall #'(lambda (x y) (+ x y)) 2 3) ==> 5
- ((lambda (x y) (+ x y)) 2 3) ==> 5
- (plot #'(lambda (x) (* 2 x)) 0 10 1)