1 / 17

CS3L: Introduction to Symbolic Programming

This lecture reviews various concepts related to lists, including list manipulation functions like cons, append, map, and member. Examples and usage of these functions are explained in detail.

Download Presentation

CS3L: Introduction to Symbolic Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS3L: Introduction to Symbolic Programming Summer 2008 Colleen Lewis colleenL@berkeley.edu Lecture 24: Review for lists, map and member

  2. Today • Midterms grades are up! Mean = 22.9/36 • Review of List stuff • list • cons • Append • member versus member? • HOFs for Lists • map • filter • reduce

  3. list • Takes any number of arguments and puts them in a list

  4. list • Examples • (list ‘cat ‘dog) ‘(cat dog) • (list ‘(cat) ‘(dog)) ‘((cat) (dog)) • (list ‘(cat) ‘dog) ‘((cat) dog) • (list ‘cat ‘(dog)) ‘(cat (dog)) • (list ‘cat ‘()) ‘(cat ()) • (list ‘() ‘dog) ‘(() dog) • (list ‘(cat ()) ‘dog) ‘((cat ()) dog)

  5. cons • Takes two arguments • Makes the first arg the car of the new list • Makes the second arg the cdr of the new list • The second argument MUST be a list

  6. cons • Examples • (cons ‘cat ‘( dog )) ‘(cat dog) • (cons ‘(cat) ‘( dog )) ‘((cat) dog) • (cons ‘cat ‘()) ‘(cat) • (cons ‘() ‘( () dog )) ‘(() () dog) • (cons ‘(cat) ‘dog ‘((cat) . dog)

  7. append • Takes two lists and turns them into one • Both arguments MUST be lists

  8. append • Examples • (append ‘(cat) ‘(dog)) ‘(cat dog) • (append ‘(cat) ‘()) ‘(cat) • (append ‘() ‘(dog)) ‘(dog) • (append ‘(cat) ‘(())) ‘(cat ()) • (append ‘(()())‘(dog)) ‘(() () dog)

  9. accessors

  10. other procedures

  11. other procedures

  12. member? versus member • For sentences • member? returns #t or #f • For lists • member returns the rest of the list after that element or #f

  13. member (member 'a '(bbb a c))  (a c) (member 'x '(bbb a c))  #f (member '(mike clancy) '((clint ryan) (mike clancy) (emily watt)) ) ((mike clancy) (emily watt)) (member ‘clancy '((clint ryan) (mike clancy) (emily watt)) )  #f

  14. map (mapprocedure list1 list2…) • procedure • a procedure that takes in some # of arguments • Some # of sents • The number of sentences MUST match the number of arguments that the procedure takes

  15. map (define (add-2-nums x y) (+ x y)) (map add-2-nums ‘(1 2 3) ‘(4 5 6)) ‘(5 7 9)

  16. map (define (add-3-nums x y z) (+ x y z)) (map add-3-nums ‘(1 2 3) ‘(4 5 6) ‘(7 8 9)) ‘(12 15 18)

  17. map (map cons ‘(1 2 3) ‘((4) (5) (6))) ‘((1 4) (2 5) (3 6)

More Related