Functional - Programming - LISP - Part 2
Functional - Programming - LISP - Part 2
LISP MANUAL
Submission
Student ID Neshat Tavangar Ranjbar
Student Name 20220001771
Instructions Try the exercises below and attach your full screenshot of your code and solution when
prompted by Run code here!
Page 1 of 16
LISP - Decision Making
Decision making structures require that the programmer specify one or more conditions to be
evaluated or tested by the program, along with a statement or statements to be executed if the
condition is determined to be true, and optionally, other statements to be executed if the
condition is determined to be false.
Following is the general form of a typical decision-making structure found in most of the
programming languages −
Page 2 of 16
If test1 evaluates to be nil, then control moves to the second clause without executing action1,
and the same process is followed.
If none of the test conditions are evaluated to be true, then the cond statement returns nil.
Exercise 1
Write a lisp code using cond statement to check if the value of variable 'a' is less than 10 and if
the value of variable 'b' is less than 10, otherwise print the value of 'a'.
Run code here!
Page 3 of 16
Page 4 of 16
LISP - If Construct
The if macro is followed by a test clause that evaluates to t or nil. If the test clause is evaluated to
the t, then the action following the test clause is executed. If it is nil, then the next clause is
evaluated.
Syntax for if −
(if (test-clause) (action1) (action2))
Exercise 2
Write a LISP code that sets the value of a to 30 and then checks if the value of a is greater than
20. If it is, print "a is greater than 20" followed by "value of a is 30".
Run code here!
Page 5 of 16
Exercise 3
Write a LISP code that sets the value of a to 5 and then checks if the value of a is greater than
20. If it is not, print "a is less than 20" followed by "value of a is 5".
Run code here!
Page 6 of 16
Exercise 4
Write a LISP code that sets the value of a to 100 and then checks if the value of a is greater than
200. If it is, print "a is greater than 200", otherwise print "a is less than 200". In either case,
print "value of a is 100".
Run code here!
Page 7 of 16
Page 8 of 16
LISP - Loops
There may be a situation, when you need to execute a block of code numbers of times. A loop
statement allows us to execute a statement or group of statements multiple times and following is
the general form of a loop statement in most of the programming languages.
Page 9 of 16
)
Exercise 5
What is the LISP code to print each element of the list '(Welcome to CUD) on a new line?
Run code here!
Exercise 6
Write a LISP code that loops from 10 to 20 with an increment of 3 and prints each number in the
loop.
Run code here!
Page 10 of 16
Exercise 7
Write a LISP code that loops from 1 to 20 and prints only the odd numbers in the loop.
Run code here!
Page 11 of 16
LISP - Functions
A function is a group of statements that together perform a task.
You can divide up your code into separate functions. How you divide up your code among
different functions is up to you, but logically the division usually is so each function performs a
specific task.
Exercise 8
Write a LISP code that defines a function "fact" that takes one argument (n1) and returns the
factorial of that number. The function should be tested by passing the arguments 4.
Run code here!
Page 12 of 16
Exercise 9
Write a Lisp program that calculates the area of a rectangle and displays its width, height, and
area. The program should take two arguments, width (w) and height (h), and use them to
calculate the area of the rectangle. The output should display the width and height, followed by
the area of the rectangle.
Run code here!
Page 13 of 16
The documentation string describes the purpose of the function. It is associated
with the name of the function and can be obtained using
the documentation function.
The body of the function may consist of any number of Lisp expressions.
The value of the last expression in the body is returned as the value of the
function.
You can also return a value from the function using the return-from special
operator.
Exercise 10
Write a Lisp program that evaluates the following polynomial expression: ax^2 + bx + c. The
program should take four arguments: coefficients a, b, and c and the value of the variable x. The
program should return the result of the polynomial expression when a, b, c, and x are
substituted with the given values.
Run code here!
Page 14 of 16
LISP - Mapping Functions
Mapping functions are a group of functions that could be applied successively to one or more
lists of elements. The results of applying these functions to a list are placed in a new list and that
new list is returned.
For example, the mapcar function processes successive elements of one or more lists.
The first argument of the mapcar function should be a function and the remaining arguments are
the list(s) to which the function is applied.
The argument function is applied to the successive elements that results into a newly constructed
list. If the argument lists are not equal in length, then the process of mapping stops upon reaching
the end of the shortest list. The resulting list will have the same number of elements as the
shortest input list.
Exercise 11
Write a Lisp program using the mapcar function that calculates the variance for a set of
numbers. The program should take two arguments, a list with any arbitrary numbers and the
mean of the list and return the variance. The program should display the variance as an output.
Hint: variance = Σ(Xi−¯X)2 / N-1 (where Xi is each element in your list and ¯X is
the mean of your list)
Page 15 of 16
Run code here!
Page 16 of 16