Finished Chapter 5 about Functions in PCL today. I read about how to handle circumstances where you're mixing different kinds of parameters. Combining optional and keyword parameters should be avoided. If the caller doesn't supply all optionals, they'll mistake the following keyword parameters as the value being supplied for the optionals. If using optionals and keywords, it's probably just best to use keyword parameters for those optionals.
I also read about Lisp's return statements. The equivalent of return
in other languages is the return-from
special operator in CL. You supply it the function name and the value you want to return. They're used far less frequently than in C-influenced languages because all Lisp forms return a value by virtue of being able to be evaluated.
Circumstances were also mentioned where you might want to use functions as data. For example, you might use this for being able to pass one function into another as a parameter. You can access a function object directly through the use of the function
special operator or #'
. When dealing with a function passed in as a variable, you'll want to use either the funcall
or apply
special operators. funcall
is good when you know the parameters before runtime. apply
on the other hand takes a list of arguments to pass into the function instead of individual parameters one after the other. This makes it good for when you don't know the parameters untill runtime.
There are also anonymous functions in Lisp. You use the lambda
operator to create them. Anonymous functions are well-suited for when defining a named function feels like overkill. They're also really nice for simple functions you're passing in as an argument which can be expressed in one or two lines.
Tomorrow I'll begin Chapter 6 which is on variables and closures.