Presentation is loading. Please wait.

Presentation is loading. Please wait.

A function (negatives L) that takes a list of numbers L and returns a list containing only negative numbers from L. A recursive example to remind you (defun.

Similar presentations


Presentation on theme: "A function (negatives L) that takes a list of numbers L and returns a list containing only negative numbers from L. A recursive example to remind you (defun."— Presentation transcript:

1 A function (negatives L) that takes a list of numbers L and returns a list containing only negative numbers from L. A recursive example to remind you (defun negatives(L) ) (cond ( ) ) ( t )(negatives ) ( ) Go through the list recursively one element at a time On each loop, check if the first element of the list is < 0 stop when? Stopping condition(null L) Function definition recursive condition2 ‘() (rest L) If current element < 0, cons that element to recursive answer (cons ) Recursive condition1(< (first L) 0) When we get to the end of the list If it is not < 0, simply pass back the recursive answer (negatives )(first L)(rest L)

2 Examples of running this function > (negatives ‘(2 –1 3 -2) ) > (negatives ‘( (2 –1 (4 -2)) -3 (1 -4) ) ) What happens if we make this call? (-1 -2) ( -3 ) We only get negatives on the top level of the list; the negatives function does not go “down into” internal lists. (defun negatives(L) (cond ( (null L) ‘() ) ( (< (first L) 0 ) (cons (first L) (negatives (rest L))) ) ( t (negatives (rest L)) ) )) Why not? Because the function doesn’t check if the first element of L is a list itself.

3 We want to write a function (all_negatives L) that finds all the negative numbers in a list and in any sublists of that list. >(all_negatives ‘( (2 –1 (4 -2)) -3 (1 -4) ) ) ( –1 -2 -3 -4 ) How should this function work? It should recurse along the list L, processing (first L) and adding to the results from (all_negatives (rest L)). What do we want the function to do with (first L)? If (first L) is a number:Check if it’s negative; if so, add to the (all_negatives (rest L)) result If (first L) is a list:Get all negatives in the list (first L) add to (all_negatives (rest L)) result How do we do this? By calling (all_negatives (first L))

4 Double recursion:two recursive calls To get all_negatives in a list L, we want to Get all_negatives in (first L) Get all_negatives in (rest L) if (first L) is a list itself normal recursion Join these two results together to give us all_negatives in the overall list An example (assume all_negatives function is working) (all_negatives ‘( (2 –1 (4 -2)) -3 (1 -4) ) ) L First Lrest L (all_negatives ‘(2 –1 (4 -2)) )(all_negatives ‘( -3 (1 -4)) ) ‘(-1 -2) ‘(-3 -4) (append ) (-1 -2 -3 -4) We make 2 recursive calls: one to get all_negatives in the (first L) list, the other to get all_negatives in the (rest L) list. Each call will return a list of negatives as its answer: join them using append.

5 (all_negatives ) (defun all_negatives(L) ) (cond ( ) ) ( t ) (all_negatives ) ( ) If (first L) is number <0? If (first L) is a list? (null L)‘() (rest L) otherwise (cons ) (< (first L) 0) (all_negatives )(first L)(rest L) Writing the function Stopping condition? ( ) (append ) (listp (first L) ) (all_negatives )(first L)(rest L) Cons (first L) to result being built If (first L) is a list we do a recursive call to get all negatives in (first L) and join them to the result being built

6 Why double recursion is simple Most of the functions that we write are designed to operate on lists. But we already know the function operates on lists; we just change the code a bit so that it can call itself on internal lists. 15 >(total_sum ‘( 2 (2 1 (3)) 2 (1 (2 ( (1) 1))) ) We usually do a double recursion when we want to handle a list within a list. Another function that needs to be doubly recursive : total_sum Gets the total sum of all numbers in a list and its sublists

7 Another example: depth in a list 0 >(max_depth ‘( a cat and a dog ) ) 1 >(max_depth ‘( a cat and (a rat and a dog)) ) 3 >(max_depth ‘( a cat and (a rat (with green eyes (that glow) ) and a dog) ) ) The maximum depth of a list is +1 the maximum depth of the sublists in that list. If there are no sublists, the depth is 0 We can use the built-in function (max X Y) which takes two numbers and returns the biggest one.

8 (defun max_depth(L) ) (cond ( ) ) ( ) (max ) One possible soln for max_depth (max_depth ) ( t ) (max_depth ) (null L)‘() (rest L) (listp (first L) ) (max_depth )(first L)(+ 1 )(rest L)

9 How far ‘down’ do these functions go? One thing that people sometimes wonder about these “doubly recursive” functions is, what happens if there’s a list inside a list inside a list? do I have to add another line to my function to handle that? The answer is no. By providing recursive calls on the first element of a list and the rest of a list, we are making a function that can handle lists inside lists. When we call that function for the first element of a list, that function can handle any sublists of that first element. It will call itself for those sublists, and so will handle any sublists of those sublists, and any sublists of those, and so on, all the way down.


Download ppt "A function (negatives L) that takes a list of numbers L and returns a list containing only negative numbers from L. A recursive example to remind you (defun."

Similar presentations


Ads by Google