Créer une présentation
Télécharger la présentation

Télécharger la présentation
## Commands and predicates

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -

**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 Commands Predicates**Sequences (1)**Sequences Lists Vectors* *Vectors are 1-dimensional array.**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…**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)**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.**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?”**find-if**• Find-ifis 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)**find-if-not**• Find-if-notsearches 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.**Filtering lists: remove**• Removereturns 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.**remove-if**• Remove-ifis 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.