Skip to content

Instantly share code, notes, and snippets.

@sliminality
Created October 17, 2016 04:34
Show Gist options
  • Save sliminality/418c4e57b8ae4e0a4494215e9e680a81 to your computer and use it in GitHub Desktop.
Save sliminality/418c4e57b8ae4e0a4494215e9e680a81 to your computer and use it in GitHub Desktop.
map and filter examples from EECS 111 recitation
(define-struct person (name age))
(define CONTACTS (list (make-person "Sarah" 20)
(make-person "Ian" 99)
(make-person "Bob" 14)
(make-person "Joe" 15)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; map example
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; age-one-year : person -> person
(define age-one-year
(lambda (p)
(make-person (person-name p)
(+ (person-age p) 1))))
; this thing...
(map age-one-year
CONTACTS)
; ...returns
; (list
; (make-person "Sarah" 21)
; (make-person "Ian" 100)
; (make-person "Bob" 15)
; (make-person "Joe" 16))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; filter examples
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; (filter <Predicate (A -> boolean)> <Listof A>)
; broken version of is-teenager?
; takes a number instead of a person
(define is-teenager-broken?
(lambda (age)
(and (>= age 13)
(<= age 19))))
; is-teenager? : person -> boolean
; returns true if the person's age is between 13 and 19
(define is-teenager?
(lambda (peep)
(and (>= (person-age peep) 13)
(<= (person-age peep) 19))))
(filter is-teenager? CONTACTS)
; ; filter starts with the list
; (list (make-person "Sarah" 20)
; (make-person "Ian" 99)
; (make-person "Bob" 14)
; (make-person "Joe" 15))
;
; ; output list so far:
; (list (make-person "Bob" 14) (make-person "Joe" 15))
;
; ; take the predicate and call it, passing in the first list item
; (is-teenager? (make-person "Sarah" 20)) ; false, don't include in output
;
; ; repeat with second item
; (is-teenager? (make-person "Ian" 99)) ; false
;
; ; and so on
; (is-teenager? (make-person "Bob" 14)) ; true => add to list
;
; ; last step
; (is-teenager? (make-person "Joe" 15)) ; true => add to list
; works with built-in predicates too
(filter even? (list 10 5 33 11))
; Below is the example where we had to use an inline lambda,
; rather than defining our predicate separately.
; above-given-age : number, (listof person) -> (listof person)
; returns only persons above the given age
(define above-given-age
(lambda (threshold-age list-of-persons)
(filter (lambda (p)
(> (person-age p) threshold-age))
list-of-persons)))
; ; The following DOESN'T work:
;
; (define above-age
; (lambda (p)
; (> (person-age p) threshold-age))) ; threshold-age isn't defined here
;
; (define above-given-age
; (lambda (threshold-age list-of-persons)
; (filter above-age ; using the function defined above, instead of an inline lambda
; list-of-persons)))
;
; ; this won't work because the above-age function can't access
; ; the threshold-age variable
; (above-given-age 15 CONTACTS)
; f(x, y) = 2x + y^2
; f(4, 2)
; 2(4) + (2)^2
; the function call
(above-given-age 15 CONTACTS)
; becomes, after substitution,
(filter (lambda (p)
(> (person-age p) 15))
(list (make-person "Sarah" 20)
(make-person "Ian" 99)
(make-person "Bob" 14)
(make-person "Joe" 15)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment