In the Lisp programming language, recursion is a commonly used technique for solving problems. Lisp is a functional programming language, which means it is well-suited to recursive solutions. In LISP, recursion is a programming technique in which a function calls itself repeatedly until a certain condition is met. It is a function that is called by itself more than once. This can be a useful way to solve large problems that can be broken down into smaller subproblems. A function is recursive if it calls itself -
- Boundary condition: then it's not recursive
- Recursive condition: must be a smaller sub-problem to converge the solution.
In recursion, the recursive function ends until its base condition is satisfied. The base condition is a must in recursion otherwise the recursive function may go into an infinite loop which may cause it to memory's stack overflow. A function that uses recursion is called a recursive function. The below figure shows the syntax and flow of the recursion in LISP.
Syntax
(defun function_name(parameters);
body-of-function
if(base-condition)
return;
body-of-function
function_name(parameters)
)
Example 1:
Lisp
(defun power (x y)
( if (= y 0)
1
(* x (power x(- y 1)))
)
)
(format t "3 to power 4 is : ~D" (power 3 4))
Output :

The above recursive power function computes 3 to power 4 i.e (3 * 3 * 3 * 3) = 81.
The Factorial of a number n is the product of all the numbers from 1 to 5. Here is a recursive implementation of a function that calculates the factorial of a given number.
Example 2:
Lisp
(defun factorial (n)
(if (= n 0)
1
(* n (factorial (- n 1))) ) )
(loop for i from 0 to n
do (format t "~A! = ~A~%" i (factorial i)) )
Output:

This function works by first checking if the input 'n' is zero. If it is, the function returns 1. If 'n' is not zero, the function calls itself with an input of (n-1), which is one less than n. This recursive call continues until 'n' is reduced to zero, at which point the recursive calls will start returning and the final result will be calculated. To calculate the factorial of 5, the function would be called with an input of 5 and would perform the following calculations and the flow of the recursive function would be:
(* 5 (factorial 4))
(* 5 (* 4 (factorial 3)))
(* 5 (* 4 (* 3 (factorial 2))))
(* 5 (* 4 (* 3 (* 2 (factorial 1)))))
(* 5 (* 4 (* 3 (* 2 (* 1 (factorial 0))))))
(* 5 (* 4 (* 3 (* 2 (* 1 1)))))
(* 5 (* 4 (* 3 (* 2 1))))
(* 5 (* 4 (* 3 2)))
(* 5 (* 4 6))
(* 5 24)
---> 120
LISP Tail Recursion
LISP also has a built-in function called 'recur' that can be used to implement Tail-recursive functions. A tail-recursive function is a special type of recursive function in which the recursive call is the last thing that the function does before returning a result. This allows the interpreter to optimize the function and avoid building up a large call stack, which can improve performance and reduce the risk of running out of stack space.
Example 3:
Lisp
(defun factorial (n acc)
(if (= n 1)
acc
(recur (- n 1) (* acc n))))
(format t "Factorial of 5 is ~D" (factorial 5 1))
Output:

This function works in the same way as the previous example, but it uses an accumulator variable 'acc' to keep track of the intermediate results. The initial value of 'acc' should be '1'.
There are many different types of problems that can be solved using recursion and traditional method using functions. A recursion is a powerful tool for solving problems in a concise way. However, it is important to use recursion carefully, as it can lead to infinite loops if not implemented correctly. Overall, recursion is a valuable technique for a programmer.
Similar Reads
Vectors in LISP
In this article, we will discuss Vectors in LISP. Vectors in LISP are one-dimensional arrays which are also known as sequences. We can create a vector using vector function and # symbol Syntax: variable_name(vector element1 element2 ... element n) or variable_name #(element1 element2 ... element n)
2 min read
Structures in LISP
LISP, is a list processing, is a programming language widely used in working with data manipulation. Structures are used defines data types, that have the ability to combine with another data type to complete the given task. Attribute used: The defstruct attribute is used to create an instance of a
2 min read
Macros in LISP
Macro is similar to a function in popular languages like C++, java, etc. that takes arguments and returns a LISP form to be evaluated. It is useful when the same code has to be executed with a few variable changes.For example, rather than writing the same code for squaring a number, we can define a
2 min read
Recursion in Ruby
The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. Recursion makes the process easier and it reduces a lot of compiling time. In Ruby, we can put all the actions in a loop, so that they can be repeate
4 min read
Numbers in LISP
LISP is a list processing programming language. It is widely used in the manipulation of data strings. It provides an input and output library. LISP provides a macro system and provides well control structures for the manipulation of data. LISP Math Function:floor:Â floor returns the nearest smalles
2 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
Strings in LISP
A string is a set of characters. String  are enclosed in double-quotes. Example: "hello geek","java","python" etc Example: LISP program to display strings Lisp ;edisplay hello geek (write-line "Hello Geek") ;display (write-line "Welcome to java") Output: Hello Geek Welcome to javaString Comparison
4 min read
Sets in LISP
A set is an unordered collection of items. A set is just like other data structures. In C++ we have a similar data structure called a hash map. Common lisp does not provide a built-in set data type, but it provides a number of functions that allow set operations to be performed onto a list. Using th
7 min read
Sequences in LISP
In Lisp, the ordered set of elements is represented by sequences. All the functionality we use in sequences is applied on vectors and lists which are two of the subtypes of sequences. Creating a Sequence:The generic function for creating a Sequence in Lisp is: ;The generic function for creating a Se
7 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