6
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? (fir st 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?

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

Embed Size (px)

Citation preview

Page 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

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?

Page 2: 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

( 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.

Page 3: 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

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 argumentsUp 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

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

nil

Page 4: 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

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 argumentsYou could also have>(apply #’equal ‘( (cat dog) (cat dog)) )

t

Funcall does the same thing with a different syntaxFuncall 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) )

Call to the funcall function

1st argument: the eq function

1st argument for eq

nil

And so on. Apply isn’t limited to comparison functions; it can take any function at all

2nd argument for eq

Page 5: 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

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.

Page 6: 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

Giving a function as an argument

(cond ( )

)

(defun member(A L )

)

( )( = A ) ( t )A

(member )

(null L) (first L)

nil

(rest l) t

Fn

Fnfuncall 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: