0% found this document useful (0 votes)
22 views

Functional - Programming - LISP - Part 2

The document discusses decision making and looping constructs in LISP including cond, if, loop for and functions. It also covers lambda functions and mapping functions. There are 11 exercises provided to write code using these LISP constructs.

Uploaded by

masa
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Functional - Programming - LISP - Part 2

The document discusses decision making and looping constructs in LISP including cond, if, loop for and functions. It also covers lambda functions and mapping functions. There are 11 exercises provided to write code using these LISP constructs.

Uploaded by

masa
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Faculty of Engineering,

Applied Science and Technology

LISP MANUAL

BCS 222 – Programming Paradigms

Functional Programming (LISP) – Part 2

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 −

LISP - Cond Construct


The cond construct in LISP is most commonly used to permit branching.
Syntax for cond is −
(cond (test1 action1)
(test2 action2)
...
(testn actionn))
Each clause within the cond statement consists of a conditional test and an action to be
performed.
If the first test following cond, test1, is evaluated to be true, then the related action part, action1,
is executed, its value is returned and the rest of the clauses are skipped over.

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.

LISP - Loop For Construct


The loop for construct allows you to implement a for-loop like iteration as most common in other
languages.
It allows you to
 set up variables for iteration
 specify expression(s) that will conditionally terminate the iteration
 specify expression(s) for performing some job on each iteration
 specify expression(s), and expressions for doing some job before exiting the loop
The for loop for construct follows several syntax −
(loop for loop-variable in <a list>
do (action)
)

(loop for loop-variable from value1 to value2


do (action)

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.

Defining Functions in LISP


The macro named defun is used for defining functions. The defun macro needs three arguments

 Name of the function


 Parameters of the function
 Body of the function
Syntax for defun is −
(defun name (parameter-list) "Optional documentation string." body)
Let us illustrate the concept with simple examples.

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!

Please note that −


 You can provide an empty list as parameters, which means the function takes no
arguments, the list is empty, written as ().
 LISP also allows optional, multiple, and keyword arguments.

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.

LISP - Lambda Functions


At times you may need a function in only one place in your program and the function is so trivial
that you may not give it a name, or may not like to store it in the symbol table, and would rather
write an unnamed or anonymous function.
LISP allows you to write anonymous functions that are evaluated only when they are
encountered in the program. These functions are called Lambda functions.
You can create such functions using the lambda expression. The syntax for the lambda
expression is as follows −
(lambda (parameters) body)
A lambda form cannot be evaluated and it must appear only where LISP expects to find a
function.

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

You might also like