Presentation is loading. Please wait.

Presentation is loading. Please wait.

Operating on Lists Chapter 6. Firsts and Seconds n Transforming list of pairs into two lists –(firsts ‘((1 5) (2 6) (3 7)))  (1 2 3) –(seconds ‘((1 5)

Similar presentations


Presentation on theme: "Operating on Lists Chapter 6. Firsts and Seconds n Transforming list of pairs into two lists –(firsts ‘((1 5) (2 6) (3 7)))  (1 2 3) –(seconds ‘((1 5)"— Presentation transcript:

1 Operating on Lists Chapter 6

2 Firsts and Seconds n Transforming list of pairs into two lists –(firsts ‘((1 5) (2 6) (3 7)))  (1 2 3) –(seconds ‘((1 5) (2 6) (3 7)))  (5 6 7) n Similar code (defun seconds (L) (unless (null L) (cons (second (first L)) (seconds (rest L))))) –replace “second” with “first” for firsts

3 Transforming a List n Programming cliché –transform a list into another list (defun (theList) (unless (null theList) (cons ( (first theList)) ( (rest theList))))) n Applies to each element – = second, for example

4 Transforming a List n Programming cliché –transform a list into another list (defun (theList) (unless (null theList) (cons ( (first theList)) ( (rest theList))))) n Applies to each element – = second, for example firsts firsts first

5 Transforming a List n Programming cliché –transform a list into another list (defun (theList) (unless (null theList) (cons ( (first theList)) ( (rest theList))))) n Applies to each element – = second, for example seconds seconds second

6 MAPCAR n A built in function for transforming lists –map each element – car to get an element (defun (theList) (mapcar #’ theList)) n Applies to each element of theList n Note the way the function appears: #’name

7 Using MAPCAR > (mapcar #’first ‘((1 5) (2 6) (3 7))) (1 2 3) > (mapcar #’second ‘((1 5) (2 6) (3 7))) (5 6 7) > (defun square (N) (expt N 2)) SQUARE > (mapcar #’square ‘(1 2 3 4 5 6 10)) (1 4 9 16 25 36 100)

8 MAPCAR vs. Recursion (defun square-all (L) (unless (null L) (cons (square (first L)) (square-all (rest L))))) n Mapcar version more concise –easier to read –more efficient –performs exactly the same task (defun square-all (L) (mapcar #’square L))

9 Exercise > (mapcar #’third ‘((1 2 3) (a b c) (do re mi))) > (mapcar #’fourth ‘((1 2 3 4 5) (x y z))) > (mapcar #’oddp ‘(1 2 3 4 5 6 7)) > (mapcar #’list ‘(a b c d e)) > (defun sum-list (L) (if (null L) 0 (+ (first L) (sum-list (rest L))))) > (mapcar #’sum-list ‘((1 2 3 4) (5 6 7) (8 5 2)))

10 “Helper” Functions n Recall the function that calculates the correlation of a list of pairs –(correlate-pairs ‘((5 3) (10 6) (15 12))) => 0.989 –used the correlation function from last time (defun correlate-pairs (XYs) (correlation (firsts XYs) (seconds XYs))) n Needed to define firsts and seconds as separate functions

11 Avoiding Helpers n Use MAPCAR and avoid having to make those functions (defun correlate-pairs (XYs) (correlation (mapcar #’first XYs) (mapcar #’second XYs)))

12 Multi-Argument MAPCAR n May take a third, fourth, …, argument n Function argument is applied to firsts of all lists, then seconds, then thirds, &so on… > (mapcar #’+ ‘(1 2 3) ‘(4 5 6) ‘(7 8 9)) (1 2 3) (4 5 6) +(7 8 9) (121518)

13 Exercise > (mapcar #’* ‘(1 2 7) ‘(3 4 6)) > (mapcar #’expt ‘(1 2 3 4 5) ‘(5 4 3 2 1)) > (mapcar #’ (mapcar #’< ‘(1 2 3 4 5) ‘(2 4 6 8 10) ‘(5 6 7 8 9)) > (mapcar #’append ‘((1 2) (a b)) ‘((x y) (do re))) > (defun distance (P1 P2) (abs (– P1 P2))) > (mapcar #’distance ‘(1 2 3 4) ‘(5 4 3 2))

14 Other List Handling Functions n Some other common tasks have been abstracted into built-in functions –remove-ifreturns a shorter list –remove-if-notreturns a shorter list –count-ifcounts matching items in the list –find-ifreturns first matching item

15 Remove-If & Remove-If-Not n Use a predicate to eliminate/select items > (remove-if #’oddp ‘(1 2 3 4 5 6 7)) (2 4 6) > (remove-if-not #’oddp ‘(1 2 3 4 5 6 7)) (1 3 5 7)

16 Functions, Not Assignments n Remove-if & remove-if-not do not modify their argument > (setf aList ‘(1 2 3 4 5 6 7)) (1 2 3 4 5 6 7) > (remove-if #’oddp aList) (2 4 6) > aList (1 2 3 4 5 6 7) Use (setf Var (remove-if … Var)) to change the value by removing items

17 Count-If n Use a predicate to count items > (count-if #’oddp ‘(1 2 3 4 5 6 7)) 4 > (count-if #’evenp ‘(1 2 3 4 5 6 7)) 3 n Equivalent to (length (remove-if-not F L)) –but more efficient

18 Find-If n Returns first item that satisfies the predicate > (find-if #’oddp ‘(2 3 4 5 6 7)) 3 > (find-if #’evenp ‘(2 3 4 5 6 7)) 2

19 Exercises > (remove-if #’numberp ‘(a 2 c (i v) 5)) > (remove-if-not #’atom ‘(a 2 c (i v) 5)) > (count-if #’symbolp ‘(a 2 c (i v) 5)) > (defun in-range (N) ( (defun in-range (N) (<= 0 N 100)) > (remove-if-not #’in-range ‘(110 75 –10 44 50)) > (find-if #’in-range ‘(110 75 –10 44 50)) > (find-if #’listp ‘(a 2 c (i v) 5))

20 Function Calling Functions n Funcall and apply call functions > (funcall #’+ 1 2 3 4 5 6) 21 > (apply #’+ ‘(1 2 3 4 5 6)) 21 n Both same as (+ 1 2 3 4 5 6)

21 Funcall, Apply & Eval n (funcall #’+ 1 2 3)=(+ 1 2 3) –four arguments –function + its arguments n (apply #’+ ‘(1 2 3))=(+ 1 2 3) –two arguments –function + list of its arguments n (eval ‘(+ 1 2 3))=(+ 1 2 3) –one argument: form to be evaluated

22 Applying Functions to Lists n Apply used to apply functions to lists –list passed as an argument, perhaps –avoid recursive function calls > (defun sum-list (L) (apply #’+ L)) > (sum-list ‘(1 2 3 4 5 6)) 21

23 APPLY vs. Recursion (defun sum-list (L) (if (null L)0 (+ (first L) (sum-list (rest L))))) n Apply version more concise –easier to read –more efficient –perform exactly the same task (defun sum-list (L) (apply #’+ L))

24 Dot Products n We defined a dot-product function last time –(dot-product ‘(5 10 15) ‘(3 6 9)) => 210 –5*3 + 10*6 + 15*9 = 15 + 60 + 135 = 210 (defun dot-product (Xs Ys) (if (null Xs) 0 (+ (* (first Xs) (first Ys)) (dot-product (rest Xs) (rest Ys)))) n Can be written using apply and mapcar –use mapcar to multiply each pair of numbers –use apply to add up the resulting list

25 Dot Product n (mapcar #’* ‘(5 10 15) ‘(3 6 9)) => (15 60 135) n (apply #’+ ‘(15 60 135)) => 210 (defun dot-product (L1 L2) (apply #’+ (mapcar #’* L1 L2)))

26 Plus 1 List Revisited n Wanted to add 1 to each element of a list (defun plus1List (L) (unless (null L) (cons (+ 1 (first L)) (plus1List (rest L))))) n Using MAPCAR: (defun plus1List (L) (mapcar #’1+ L)) –1+ is the “add 1” function

27 Plus 2 List n Suppose we wanted to add 2 instead (defun plus2List (L) (unless (null L) (cons (+ 2 (first L)) (plus1List (rest L))))) n There is no 2+ function –need to write our own (defun 2+ (N) (+ N 2)) –how can we avoid that?

28 Lambda ( ) in LISP n Lambda expression is a way of specifying a function without giving it a name n Very much like defun – leave out the name (defun add-10 (x) (+ x 10)) (lambda (x) (+ x 10)) n Both give the same function –but defun gives it a name, too

29 Using Lambda Expressions n Can be used where-ever a function is –function arguments, in particular > (mapcar (lambda (x) (+ x 10)) ‘(1 2 3 4)) (11 12 13 14) > ((lambda (x) (+ x 10)) 5) 15 n Note: text has #’ in front of the expr. –may be necessary in some LISPs

30 Exercises n Evaluate the following: > (mapcar (lambda (x) (* x x)) ‘(2 4 6 8)) > ((lambda (x) (numberp x)) ‘(a 2 c (i v) 5)) > (mapcar (lambda (x) (and (numberp x) (evenp x))) ‘(a 2 c (i v) 5))

31 Exercise n Write a function using mapcar and a lambda expression to double every element of a list > (double-all ‘(3 5 8 18)) (6 10 16 36)

32 Lambda Variables n Lambda expressions use variables –parameter list –“open” variables n Looks for nearest definition of the variable –a parameter of this lambda expression –a parameter of an enclosing lambda/defun

33 Open Variables n We can write a function that adds a given amount to every element of a list > (add-to-each 5 ‘(2 4 6 9)) (7 9 11 14) n Can use first parameter as “open” in lambda > (defun add-to-each (Amt Lst) (mapcar (lambda (x) (+ Amt x)) Lst))

34 Exercise n Write a function using mapcar and a lambda expression that calculates the N th power of every element of a list (where N is given as the second parameter) > (nth-power-each ‘(1 2 3 4 5) 3) (1 8 27 64 125)

35 Exercise n Defun this function: –(mathify #’+ 5 ‘(1 2 3 4));; add 5 to each (6 7 8 9) –(mathify #’* 4 ‘(1 2 3 4));; multiply each by 4 (4 8 12 16) –(mathify #’/ 10 ‘(1 2 3 4));; divide each by 10 (1/10 1/5 3/10 2/5) –break it into steps!

36 Next Time n Data Abstraction –chapter 6


Download ppt "Operating on Lists Chapter 6. Firsts and Seconds n Transforming list of pairs into two lists –(firsts ‘((1 5) (2 6) (3 7)))  (1 2 3) –(seconds ‘((1 5)"

Similar presentations


Ads by Google