Download presentation
Presentation is loading. Please wait.
Published byBritton Montgomery Modified over 9 years ago
1
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 (cons ) (cond ( ) ) ( t )(list ) (recursive-call? ) (null L) ‘() Num? (first l) 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?
2
( t ) Function index_help (cons )(list ) (index_help ) (defun index_help (L N) ) (cond ( ) ) (null L) ‘() N (first L )(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 This is a function to help the index_items function.
3
It then evaluates (eq ‘(cat dog) ‘(cat dog)), and returns the answer Apply puts its first arg #’eq at the front of its second arg (list of args to eq), giving (eq ‘(cat dog) ‘(cat dog)) 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) ) ) Call to the apply function 1 st argument: the eq function 2 nd 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 nil
4
It then evaluates (eq ‘(cat dog) ‘(cat dog)), and returns the answer funcall puts its first arg #’eq at the front of a list of all the other arguments, giving (eq ‘(cat dog) ‘(cat dog)) More functions as arguments You could also have >(apply #’equal ‘( (cat dog) (cat dog)) ) t 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 2 nd arg, the 3 rd arg and so on >(funcall #’eq ‘(cat dog) ‘(cat dog) ) Call to the funcall function 1 st argument: the eq function 1 st argument for eq nil And so on. Apply isn’t limited to comparison functions; it can take any function at all 2 nd argument for eq
5
What is the point of this? This definition of member uses = to compare elements. It allows us to write more flexible functions. For example: (cond ( ) ) ( t ) (defun member(A L) ) ( ) A(member ) (null L) (first L) nil (rest l) t( = A ) But we might want to use eq sometimes, eq or equal equal = We write a new version of member that takes, as an argument, whichever comparison function we want to use.
6
Giving a function as an argument (cond ( ) ) (defun member(A L ) ) ( )( = A ) ( t )A(member ) (null L) (first L) nil (rest l) t Fn funcall Fn (We use funcall but could have used apply if we wanted) >(member ‘( dog cat) ‘( (cat mouse) (dog cat)) #’eq) nil >(member ‘( dog cat) ‘( (cat mouse) (dog cat)) #’equal) t Here’s our earlier member function: Now we add in stuff to pass a function as an argument We call this like before, but giving a function also:
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.