@@ -198,6 +198,7 @@ The programmer can also define their own special forms using =special-lambda= an
be explained later.
* Symbols and keywords
* Truthyness
* Lambdas
Slime allows for creating anonymous functions called *lambdas*. We did not talk about binding
variables, we will do this in [[Define]], but we can still use lambdas now. Remember that Lisp
@@ -327,18 +328,19 @@ call to that function will look like.
** Functions with keyword arguments
A sometimes more convenient way of passing arguments to a function is using keyword arguments. Using
keyword arguments a function call could look like this: \[\texttt{(function :arg1 value1 :arg2
value2)}\] here the function accepts two arguments named =arg1= and =arg2=. The user of this
keyword arguments a function call could look like this: \[\texttt{(function :arg1 value1 :arg2
value2)}\] here the function accepts two arguments named =arg1= and =arg2=. The user of this
function can see more clearly excatly which argument will be assigned wich value. This notation also
allows for switching the argument order. The following function call is equivalent to the call
allows for switching the argument order. The following function call is equivalent to the call
above. \[\texttt{(function :arg2 value2 :arg1 value1)}\].
For this to work however, the function must be defined to accept these keyword arguments. To do this
the special marker =:keys= has to be inserted into the argument list of a =lambda= or a function
=define=. All following arguments *must* be supplied as keyword arguments, /unless/ they are also
supplied with a default value, in which case they do not need to be supplied. To attach a default
value to a keyword argument, insert =:defaults-to <value>= after the keyword argument name. An
example of all of this can be seen in [[code:keyword-args]].
=define=. All following arguments *must* be supplied as keyword arguments, /unless/ they are also
supplied with a default value, in which case they do not need to be supplied. To attach a default
value to a keyword argument, insert =:defaults-to <value>= after the keyword argument name. An
example of all of this can be seen in [[code:keyword-args]]. Important note: keyword arguments must be
defined and supplied after all the regular arguments.
{{{slime_header}}}
#+name: code:keyword-args
@@ -361,10 +363,41 @@ example of all of this can be seen in [[code:keyword-args]].
: 54
: 54
** Functions with rest arguments
If the programmer wants to create a function that can accept any number of arguments, they can use
the =rest= argument. It is defined after the special marker =:rest= and after the rest argument, no
other arguments can be defined. In the execution of the fuction, the rest arguent will be assigned
to a list containing all the supplied values. The rest argument can be used in conjunction with the
other argument types, regular arguments and keyword arguments.
{{{slime_header}}}
#+name: code:rest-args
#+caption: A more complex functoin definition using keyword arguments
#+begin_src slime
(define (execute-operation operation
:keys
do-logging :defaults-to ()
:rest values)
(define result (apply operation values))
(when do-logging
(printf "Executing operation"
operation
"agains values yielded:"
result))
result)
(printf (execute-operation '+ 1 2 3))
(printf (execute-operation '*
:do-logging t
10 11))
#+end_src
#+RESULTS: code:rest-args
: evaluates to =>
: 6
: Executing operation [C-function] agains values yielded: 110
: 110
** Functions with rest arguments
* Environments
* Built-in functions