teachscheme, reachjava adelphi university wednesday morning june 24, 2008
TRANSCRIPT
TeachScheme, ReachJava
Adelphi University
Wednesday morning
June 24, 2008
Review• definition by choices• definition by parts• char, symbol, key & mouse handlers• posns• defining other structs• definition by choices, revisited (unions)• lists (2 choices, one of which has 2 parts, one of
which is the orig. type)• structural recursion: apply already-learned coding
patterns to self-referential type
ListsA list is either• empty, or• (cons element-type list)
(define (function-on-list L)(cond [(empty? L) … ] [(cons? L) ; (first L) element-type ; (rest L) list ; (function-on-list (rest L)) whatever
Counting; count-dolls : list-of-strings -> number
"Examples of count-dolls:"
(check-expect (count-dolls empty) 0)
(check-expect (count-dolls (cons "ball" empty)) 0)
(check-expect (count-dolls (cons "doll" empty)) 1)
(check-expect (count-dolls (cons "ball" (cons "doll" (cons "doll" (cons "bat" (cons "doll" empty)))))) 3)
You try this one.
Hint: you'll need a "cond" inside the "cons?" case.
My answer; count-dolls : list-of-strings -> number(define (count-dolls toys) (cond [(empty? toys)
; toys an empty list 0 ; from one of the test cases
] [(cons? toys)
; toys a non-empty list of strings ; (first toys) a string ; (rest toys) a list of strings ; (count-dolls (rest toys)) a number
; (string=? (first toys) "doll") a boolean (cond [(string=? (first toys) "doll")
(+ 1 (count-dolls (rest toys)))] [else (count-dolls (rest toys))])
]))
A slightly different approach; count-dolls : list-of-strings -> number(define (count-dolls toys) (cond [(empty? toys)
; toys an empty list 0 ; from one of the test cases
] [(cons? toys)
; toys a non-empty list of strings ; (first toys) a string ; (rest toys) a list of strings ; (count-dolls (rest toys)) a number
(+ (1-if-doll (first toys)) (count-dolls (rest toys)))
])); "helper" function 1-if-doll : string -> number; returns 1 if the string is "doll", 0 if it's anything else.
A simpler syntax; list : object … -> list
(list "ball" "doll" "doll" "bat" "doll")is equivalent to (cons "ball" (cons "doll" (cons "doll" (cons "bat" (cons "doll"
empty)))))
Set Language -> Beginning Student with List Abbreviations
Now output will be in "list" syntax rather than "cons" syntaxNo internal difference; any function that worked before will
still work
List exercisesWrite a function add-positives which takes in a list of
numbers and computes the sum of the positive ones, ignoring any zeroes or negatives.
Write a function largest which takes in a non-empty list of numbers and returns the largest of them. (Hint: need to define new data type "non-empty list of numbers". Either one element, i.e. (cons # empty), or more than one, i.e. (cons # nelon).)
Functions producing listsWrite a function add-1-to-each which takes in a list of
numbers and produces a list of numbers, each 1 more than the corresponding number in the input.
Write a function convert-grades which takes in a list of numeric grades and produces a list of corresponding letter grades ("A", "B", etc.) in the same order.
Write a function substitute which takes in two strings (old and new) and a list of strings, and produces the same list with all occurrences of old replaced with new.
Functions producing listsWrite a function keep-positives which takes in a list of
numbers and returns a list of only the positive numbers in the list, in the same order, omitting negatives and zeroes.
Write a function sort which takes in a list of numbers and returns a list of the same numbers in increasing order. (Hint: don't worry about "what sorting algorithm"; just follow the recipe. You'll need an auxiliary function; again, just follow the recipe.)
Extra credit: lists of listsWrite a function permutations which takes in a list (of
anything) and returns a list of lists, comprising all the possible orderings of the original list. For example,(check-expect (permutations (list 1 2 3)) (list (list 1 2 3) (list 1 3 2) (list 2 1 3) (list 2 3 1) (list 3 1 2) (list 3 2 1)))
Write a function subsets which takes in a list (of anything) and returns a list of lists, comprising all the subsets of the original list (don't worry about order). For example,(check-expect (subsets (list 1 2 3)) (list (list) (list 3) (list 2) (list 2 3) (list 1) (list 1 3) (list 1 2) (list 1 2 3)))
Lists of structsAn employee-list is either• empty, or• (cons employee employee-list)
An employee is…
See employees.scm as starting pointSince an emp-list has a part which is an emp,
function-on-emp-list calls function-on-emp on that part
Lists of structs• Write total-salary which takes in a list of employees & adds up their
salaries
• Write any-over-100k? which takes in a list of employees & tells whether any of them earn over $100,000/year
• Write count-over-100k which takes in a list of employees & tells how many of them earn over $100,000/year
• Write give-10%-raises which takes in a list of employees & returns a similar list in which everybody's salary has increased by 10%
• Write fire-over-100k which takes in a list of employees & returns a list of the ones who don't earn over $100,000/year.
• Write highest-paid, which takes in a non-empty list of employees & returns the highest-paid one. (If there's a tie, return the first of the highest-paid employees.)
Lists of unionsAn animal-list is either• empty, or• (cons animal animal-list)
An animal is either• a dog, or• a fishA dog is… A fish is…
See animals.scm as a starting pointSince a list of animals has a part which is an animal, function-on-
animal-list calls function-on-animal (which in turn calls function-on-dog, function-on-fish, etc.)
Lists of unions
• Write count-tigers, which takes in a list of animals and returns how many of them are tigers
• Write extract-fish, which takes in a list of animals and returns a list of only the fish, in the same order they were in before
• Write lookup-age, which takes in a list of animals and a string, and if there is a dog with that name in the list, returns the dog's age. (If there's more than one, use the first one you find.) If not, return -1.