Keyword Parameters in LISP
Last Updated :
29 Oct, 2021
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
Keyword Parameters:
Keyword parameters are used when we want to pass a specific value to a specific parameter. Consider a scenario where there are 3 optional parameters, what if the user wants to pass just a second optional parameter's value, here the first parameters even though is optional it will be mandatory for him to pass its value making it a required parameter, to solve this problem keyword parameters are used where the caller has a choice to specify which value will be specified to which particular parameter.
The syntax for defining Keyword Parameters:
To define keyword parameters in a function, add the symbol &key before any parameters that are supposed to be set as keyword parameters.
To illustrate this look at the code below:
(defun keyword_example (&key x y z)...)
Here x y z are keyword parameter specifiers.
Example:
Now let's create a function that just has keyword parameters and also set their default values to some predefined values.
(defun keyword_example (&key (x 5) (y 7) (z (+ x y)))...)
In this function called "keyword_example" we have 3 keyword parameters,
In addition to that, we have set default values for all those parameters as for x -> 5, for y -> 7, and for z we are giving it the value of the sum of x and y
Now, for illustration purposes, in the body of this function, we will create a list that will contain our parameters
(list x y z)
Let's call this function by having various calls with different values for each parameter
Code :
Lisp
(defun keyword_example (&key (x 5) (y 7) (z (+ x y)))
(list x y z))
(write(keyword_example :x 1))
(terpri)
(write(keyword_example :y 1) )
(terpri)
(write(keyword_example :y 1 :z 4) )
(terpri)
(write(keyword_example :y 1 :z 4 :x 2 ))
Output:
(1 7 8)
(5 1 6)
(5 1 4)
(2 1 4)
Here, at the first call, we just passed the value for x by specifying its value with :x symbol, for the later function calls we set values for different parameters specifically, Also there is no need to pass the arguments in a specific order as we are using keyword specifiers(see 4th function call) thus with keyword parameters, we were able to give a specific parameter it's a specific value.
Similar Reads
Non-linear Components
In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Class Diagram | Unified Modeling Language (UML)
A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Spring Boot Tutorial
Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Backpropagation in Neural Network
Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
Steady State Response
In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
Polymorphism in Java
Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
AVL Tree Data Structure
An AVL tree defined as a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees for any node cannot be more than one. The absolute difference between the heights of the left subtree and the right subtree for any node is known as the balance factor of
4 min read
What is Vacuum Circuit Breaker?
A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
3-Phase Inverter
An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
What is a Neural Network?
Neural networks are machine learning models that mimic the complex functions of the human brain. These models consist of interconnected nodes or neurons that process data, learn patterns, and enable tasks such as pattern recognition and decision-making.In this article, we will explore the fundamenta
14 min read