



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Modern Programing Language is about different languages of today era. It explains pros and cons of some new languages and their differences with old ones. Languages like java, c sharp, c plus plus, c, fotran are included in this course. This lecture handout is about: Defun, Power, Recursion, Function, Intersection, Iteration, Dotimes, Dolist, Product, Grandfather
Typology: Exercises
1 / 6
This page cannot be seen from the preview
Don't miss anything!




Recursion is the main tool used for iteration. In fact, if you don‟t know recursion, you won‟t be able to go too far with LISP. There is a limit to the depth of the recursion, and it depends on the version of LISP. Following is an example of a recursive program in LISP. We shall be looking at a number of recursive functions in the examples.
(defun power (x y) (if (= y 0) 1 ( x (power x (1- y)))))*
This function computes the power of x to y. That is, it computes xy^ by recursively multiplying x with itself y number of times.
So
(power 3 4) 81
Let us look at another example. In this example, we compute the length of a list, that is, we compute the number of elements in a list. The function is given as follows:
(defun length (x) (if (null x) 0 (+ length (rest x) 1) ) )
(Length „(a b c d)) 4
Here are some more examples: The first function determines whether a symbol is present in a list or not and the second function computes the intersection of two lists. These functions are given below:
(defun member (x L) ; determine whether x is in L or not (cond ((null L) nil) ; base case 1: L is empty ((equal x (car L)) L) ; base case 2: x=first(L) (t (member x (cdr L))) ; recursion: test if x is in rest(L) ))
(defun intersection (L1 L2) ; compute intersection of two lists (cond ((null L1) nil) ((null L2) nil) ((member (car L1) L2) (cons (car L1) (intersection (cdr L1) L2))) (t (intersection (cdr L1) L2)) ))
It may be noted that the intersection function is different from the mathematical definition of set intersection. In this case the function looks at all the elements of the first list, one at a time, and checks whether that element is present in the second list. You may recall from
our earlier discussion that our definition of set if different from the mathematical definition of a set where duplications are not allowed. This concept is elaborated with the help of the following examples where you pass same lists in different order and get different results.
(intersection '(a b c) '(b a b c)) (a b c)
(intersection '(b a b c) '(a b c)) (b a b c)
Following is yet another example to compute the set difference of two sets.
(defun set-difference (L1 L2) (cond ((null L1) nil) ((null L2) L1) ((not (member (car L1) L2)) (cons (car L1) (set-difference (cdr L1) L2))) (t (set-difference (cdr L1) L2)) ))
Iteration : dotimes and dolist
Apart from recursion, in LISP we can write code involving loops using iterative non- recursive mechanism. There are two basic statements for that purpose: dotimes and dolist. These are discussed in the following paragraphs.
dotimes
dotimes is like a counter-control for loop. Its syntax is given as below:
( dotimes (count n result) body)
It executes the body of the loop n times where count starts with 0, ends with n-1.
The result is optional and is to be used to hold the computing result. If result is given, the function will return the value of result. Otherwise it returns NIL. The value of the count can be used in the loop body.
dolist
The second looping structure is dolist. It is used to iterate over the list elements, one at a time. Its syntax is given below:
( dolist (x L result) body)
(setf (get „a „weight) 20) ; setting the weight property of symbol a. 20
Now we list all properties of a :
(symbol-plist „a) (WEIGHT 20 HEIGHT 8)
We can remove a property by using the remprop function as shown below:
(remprop „a „WEIGHT) T
(symbol-plist „a) (HEIGHT 8)
(remprop „a „HEIGHT) T
(symbol-plist „a) NIL
We can use the property list to build more meaningful functions. Here is one example:
Assume that if the name of a person‟s father is known, the father‟s name is given as the value of the father property. We define a function GRANDFATHER that returns the name of a person‟s paternal grandfather, if known, or NIL otherwise. This function is given below:
(defun grandfather (x) (if (get x „father) (get (get x „father) „father) nil))
We now make a list of the paternal line:
(defun lineage (x) (if x (cons (x (lineage (get x „father))))))
The following would trace the family line from both father and mother‟s side:
(defun ancestors (x) (if x
(cons x (append (ancestors (get x „father)) (ancestors (get x „mother))))))
Arrays
Although the primary data structure in LISP is a list, it also has arrays. These are data type to store expressions in places identified by integer indexes. We create arrays by using the linear- array function as shown below:
(setf linear-array (make-array „(4)))
This statement creates a single dimension array of size 4 by the name of linear-array with indices from 0 to 3.
Here is an example of a two-dimensional array:
(setf chess-board (make-array „(8 8)))
Once created, we can store data at the desired index as follows:
(setf (aref chess-board 0 1) „WHITE-KNIGHT) (setf (aref chess-board 7 4) „BLACK-KING)
Here, aref is the array reference. The above statements say that store symbol WHITE- KNIGHT at chess-board position 0 1 and store BLACK-KING at position 7 4.
aref can be used to access any element in the array as shown below:
(aref chess-board 0 2) WHITE-BISHOP
(aref chess-board 7 2) BLACK-BISHOP
What made Lisp Different?
LISP was one of the earliest programming language. It was designed at MIT for artificial intelligence and it has since been the defacto standard language for the AI community, especially in the US.
LISP program composed of expression. They are in fact trees of expression, each of which returns a value. It has Symbol types: symbols are effectively pointer to strings stored in a hash table. One very important aspect of LISP is that the whole language is there. That is, there is no real distinction between read-time, compile-time and runtime. You can compile or run code while reading, read or run code while compiling, and read or compile code at runtime. That is, programs are expressed directly in the parse trees that get build behind the scenes when other languages are parsed, and these trees are make of lists, which are Lisp date structures. This provides a very powerful feature allowing the programmer to write programs that write programs.