LISP Function's parameters list has a basic aim of declaring the variables which will receive the arguments that are passed in the function. Normally parameter list contains a basic list of variable names, those parameters are called "required parameters". If a function is called, for every required parameter it should be supplied with one individual argument, if it's called with more or fewer arguments, LISP will show an error.
Types of parameters in the parameters list
- Required parameters
- Optional Parameters
- Rest Parameters
- Keyword Parameters
Rest Parameters
Sometimes a function needs to take a variable number of arguments, for illustration, take the + function in LISP which takes the variable number of arguments and is not just limited to 2 arguments, possible calls for the + function includes :
(+)
(+ 9)
(+ 5 2)
(+ 3 7 1)
So as you can see the parameter list for + function may look similar to this :
(defun + (&rest numbers) ...)
But you may doubt that we can also write this functions which would take variable number of arguments by having multiple optional parameters, but such incredible number of handling parameters is not considered a right LISP attiquette.
The syntax for defining Rest Parameters:
To define any rest parameter in the function, add the symbol: &rest before the name of the parameter, When you call a function all the arguments after the required and optional parameters are grouped together into a list and they become the value for &rest parameter.
Example:
Let's create a function where r1 & r2 will be required parameters and anything other than that would be rest parameters.
(defun rest_example (r1 r2 &rest rest)...)
Now as we know any arguments after the required and optional parameters are grouped into a list of rest parameters, So lets print those rest parameters along with our required parameters.
(format t "Required parameters: ~d & ~d.~%" r1 r2)
(format t "Rest parameters: ~d~%" rest)
Now let's call this function by passing just required parameters and second time by passing with rest parameters also
(rest_example 50 80)
(rest_example 40 60 75 67 10 30)
Code :
Lisp
(defun rest_example (r1 r2 &rest rest)
(format t "Required parameters: ~d & ~d.~%" r1 r2)
(format t "Rest parameters: ~d~%" rest))
(rest_example 50 80)
(terpri)
(rest_example 40 60 75 67 10 30)
Output :
Required parameters: 50 & 80.
Rest parameters: NIL
Required parameters: 40 & 60.
Rest parameters: (75 67 10 30)
As you may see at our first call we don't pass any rest parameters hence the value of the rest parameter was NIL, but when we call the function with rest parameters all our arguments other than the required parameters are grouped into a list of rest parameters.
Similar Reads
Optional Parameters in LISP Optional Parameters are the parameters that are optional in the function. We can put optional parameters whenever the arguments are not necessary. If we keep the optional parameters in a function and pass the values, then the values are taken in place of optional parameters. If values are not passed
2 min read
Operators in LISP Operators are the foundation of any programming language. Thus the functionality of the LISP programming language is incomplete without the use of operators. We can define operators as symbols that help us to perform specific mathematical and logical computations on operands. In other words, we can
5 min read
Predicates in LISP In this article, we will discuss predicates. Predicates are similar to functions that will be used to test their arguments for conditions. They will return NIL if the conditions are not met, if the conditions are met, they will return T. Types of predicates: Below is a list of major Predicates with
5 min read
Program Structure in LISP The expressions we use in Lisp are known as the s-expressions which are composed of atoms, strings, and lists. An s-expression is itself a valid program. REPL:REPL is an acronym for Read Evaluate Print Loop. In the case of an interpreter, the program is read, checked for errors in loops, and the val
2 min read
Variables in LISP Similar to other languages, in LISP variables are named places that store a particular value, but they are not declared the way you declare variables in C++ or Java i.e you don't need to mention data-type of variables when declaring them as LISP is dynamically typed. LISP supports two types of varia
3 min read