1 / 15

TeachScheme, ReachJava

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)

halk
Download Presentation

TeachScheme, ReachJava

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. TeachScheme, ReachJava Adelphi University Wednesday morning June 24, 2008

  2. 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

  3. Lists A 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

  4. 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.

  5. 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))]) ]))

  6. 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.

  7. 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" syntax No internal difference; any function that worked before will still work

  8. List exercises Write 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).)

  9. Functions producing lists Write 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.

  10. Functions producing lists Write 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.)

  11. Extra credit: lists of lists Write 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)))

  12. Lists of structs An employee-list is either • empty, or • (cons employee employee-list) An employee is… See employees.scm as starting point Since an emp-list has a part which is an emp, function-on-emp-list calls function-on-emp on that part

  13. 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.)

  14. Lists of unions An animal-list is either • empty, or • (cons animal animal-list) An animal is either • a dog, or • a fish A dog is… A fish is… See animals.scm as a starting point Since 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.)

  15. 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.

More Related