60 likes | 123 Views
(cond ( ) ). (null L). ‘(). ( t ).
E N D
(cond ( ) ) (null L) ‘() ( t ) (cons ) (list ) Num? (first l) (recursive-call? ) Helper functions: when extra arguments are needed Consider this problem: we want a function index_items that takes a list L and gives a number to each element in L >(index_items ‘(a b c d) ) ( (1 a) (2 b) (3 c) (4 d) ) This is a recursive function where on each recursive call a pair (list) is formed and cons’ed onto the recursive answer This is familiar from the previous lectures. But where does the number Num? Come from? How do we get the sequence of numbers with which to index each element?
(defun index_help (L N) ) (cond ( ) ) (null L) ‘() Function index_help This is a function to help the index_items function. ( t ) (cons ) (list ) N (first L) (index_help ) (rest L) (+ N 1) This index_help function takes a list L and a number N and pairs the first element of L with N, then increases N and recurses to the rest of the list L Index_items can use this helper function: (defun index_items (L) (index_help L 1) ) This definition will use index_help to index items starting at 1 and incrementing for each element in list L
Call to the apply function 1st argument: the eq function 2nd argument: list of arguments to which eq will be applied When a function is referred to like this, it always has #’ before its name, to show it’s a function and not just the word eq Functions as arguments Up to now, we’ve been writing functions that take data (lists, numbers etc.) as arguments. It’s useful sometimes to have functions that take other functions as arguments. Two built-in functions that do this are apply and funcall. Apply takes two arguments; a function, and the arguments to be given to that function. >(apply #’eq ‘( (cat dog) (cat dog) ) ) nil Apply puts its first arg #’eq at the front of its second arg (list of args to eq), giving (eq ‘(cat dog) ‘(cat dog)) It then evaluates (eq ‘(cat dog) ‘(cat dog)) , and returns the answer
Call to the funcall function 1st argument: the eq function 1st argument for eq 2nd argument for eq More functions as arguments You could also have >(apply #’equal ‘( (cat dog) (cat dog)) ) t And so on. Apply isn’t limited to comparison functions; it can take any function at all Funcall does the same thing with a different syntax Funcall takes as it’s arguments a function name, the first argument to that function, the 2nd arg, the 3rd arg and so on >(funcall #’eq ‘(cat dog) ‘(cat dog) ) nil funcall puts its first arg #’eq at the front of a list of all the other arguments, giving (eq ‘(cat dog) ‘(cat dog)) It then evaluates (eq ‘(cat dog) ‘(cat dog)) , and returns the answer
(defun member(A L) ) (cond ( ) ) (null L) nil ( ) ( = A ) (first L) t ( t ) (member ) A (rest l) What is the point of this? It allows us to write more flexible functions. For example: equal = eq This definition of member uses = to compare elements. But we might want to use eq sometimes, or equal We write a new version of member that takes, as an argument, whichever comparison function we want to use.
(defun member(A L ) ) (cond ( ) ) (null L) nil ( ) ( = A ) (first L) t ( t ) (member ) A (rest l) Giving a function as an argument Here’s our earlier member function: Fn funcall Fn Fn Now we add in stuff to pass a function as an argument (We use funcall but could have used apply if we wanted) We call this like before, but giving a function also: >(member ‘( dog cat) ‘( (cat mouse) (dog cat)) #’eq) nil >(member ‘( dog cat) ‘( (cat mouse) (dog cat)) #’equal) t