Presentation is loading. Please wait.

Presentation is loading. Please wait.

Commands and predicates LISP functions are divided into 2 classes. Predicates are functions that return boolean values i.e. t or nil. The rest are commands.

Similar presentations


Presentation on theme: "Commands and predicates LISP functions are divided into 2 classes. Predicates are functions that return boolean values i.e. t or nil. The rest are commands."— Presentation transcript:

1 Commands and predicates LISP functions are divided into 2 classes. Predicates are functions that return boolean values i.e. t or nil. The rest are commands. Functions CommandsPredicates

2 Sequences (1) Sequences ListsVectors* *Vectors are 1-dimensional array.

3 Sequences (2) Sequences contain elements which are ordered. Nil is taken to be a sequence of length zero. Common Lisp provides a set of functions to manipulate sequences. We have seen some of these functions in the previous session, such as length, reverse…

4 Searching lists: find (1) This function is used to search for the occurrence of a particular element within a list. Syntax: (find element list). Example: (find 10 ‘(1 2 3)) (if (find ‘a ‘(a b c)) ‘Yes ‘No)

5 find (2) To specify a function to use when comparing the entry to each element of the list, you should use :test (find 3 ‘(2 4 6 8) :test #’<) The above expression will return the first element of the specified list that satisfies the function ‘>’. It will return 4.

6 find (3) To tell LISP which part of each element of the list to compare to the supplied entry, use :key (find ‘A ‘((A B) (C D E) (F G H I)):key #’first) To put it in words, the above statement means that “is there any element within the supplied nested list whose first element is the atom A?”

7 find-if Find-if is used to find an element of a list that satisfies some predicate. Syntax: (find-if predicate list) For example: (find-if #’evenp ‘(1 2 3 4)) This will return the first element of the given list that satisfies the predicate evenp. Another example: (find-if #’oddp ‘((0 1) (1 2)) :key #’first)

8 find-if-not Find-if-not searches for the first element of a given list that fails a specified predicate. Try replacing the find-if in the previous example with find-if-not.

9 Filtering lists: remove Remove returns a new list similar to the supplied list, except that all the occurrences of the given entry removed. (remove 1 ‘((0 1) (1 2) (2 3)) :key #’first) The above expression will remove from the parent list, all child lists whose first element equals 1.

10 remove-if Remove-if is identical to the above, except that you would supply a predicate, and all elements within the supplied list that satisfies that predicate will be removed. (remove-if #’evenp ‘(1 2 3 4 5)) The above statement will remove all occurrences of even numbers from the given list.


Download ppt "Commands and predicates LISP functions are divided into 2 classes. Predicates are functions that return boolean values i.e. t or nil. The rest are commands."

Similar presentations


Ads by Google