Open In App

Lambda Functions in LISP

In this article, we will discuss lambda functions in LISP. The Lambda function is used to evaluate a mathematical expression in our program. They are also known as anonymous functions.

We can create these functions using lambda expression.



Syntax:

(lambda (parameters) expression_code)

where,



  1. The parameters are the numbers of operands in the expression
  2. The expression_code is the mathematical logic expression

Example 1: LISP program to evaluate the mathematical expression through a lambda expression




;lambda expression to get sum of product of four numbers
;mathematical expression is (val1*val2) + (val3*val4)
  
(write ((lambda (val1 val2 val3 val4)
   (+  (* val1 val2) (+ (* val3 val4))))
   ;pass the values
   2 4 6 8)
)
(terpri)
(write ((lambda (val1 val2 val3 val4)
   (+  (* val1 val2) (+ (* val3 val4))))
   ;pass the values
   10 20 30 40)
)

Output:

56
1400

Example 2: LISP Program to evaluate an expression




;lambda expression to get product of two numbers
;mathematical expression is (val1*val2) 
  
(write ((lambda (val1 val2 )
   (* val1 val2))
   ;pass the values
   60 4 )
)
(terpri)
(write ((lambda (val1 val2 )
   (* val1 val2))
   ;pass the values
   10 20 )
)

Output:

240
200

Article Tags :