1 / 44

TeachScheme, ReachJava

Explore primitive data types like number, string, boolean, images, along with defining composite types by parts or choices. Learn to work with lists and functions on defined types. Discuss programming concepts and coding patterns.

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 afternoon July 14, 2010

  2. Review: kinds of data types Primitive data types number, string, boolean, image, … Types defined by their parts posn, dog, fish, circle, rectangle, … Types defined as one of several choices subranges of numbers, animal, shape, … TeachScheme, ReachJava 2010

  3. Review: definition by parts ; A fish has a string(color), number(weight), and boolean(salt-water?) (define-struct fish (color weight salt-water?)) ; constructor, getters, and discriminator defined automagically Testing pattern:call (make-fish …) to create as many instances as necessary Coding pattern: (define (function-on-fish the-fish) ; the-fish fish ; (fish-color the-fish) string ; (fish-weight the-fish) number ; (fish-salt-water? the-fish) boolean ) TeachScheme, ReachJava 2010

  4. Review: definition by choices ; An animal is either a dog, a fish, or a wombat Not enforced by language; programmer convention Testing pattern:at least one example for each choice Coding pattern:(define (function-on-animal the-animal) (cond [(dog? the-animal) ; the-animal a dog ] [(fish? the-animal) ; the-animal a fish ] [(wombat? the-animal) ; the-animal a wombat ])) TeachScheme, ReachJava 2010

  5. Just to make sure… … please take some time to work the shapes-lab exercises. When are you defining a type by parts? When are you defining a type by choices? How do you write a function on a type defined by parts? How do you write a function on a type defined by choices? TeachScheme, ReachJava 2010

  6. Discussion break • In imperative programming, choose functions/procedures first and then select data types to support them. (Most user-defined types postponed to CS2.) • In OOP, define data types (classes) first and then write methods to work on them. • In our curriculum, we define data types first and then discuss how to write functions on them. TeachScheme, ReachJava 2010

  7. New topic: lists What is a list? Examples: shopping list list of students in class list of animals in zoo Properties: can hold many objects, or one, or even none number of objects may not be known in advance objects are in one-after-another sequence TeachScheme, ReachJava 2010

  8. So what is a list? • A list is either empty or not.Definition by choices: empty or not? • If it's not empty, that means it has at least one thing in it, so there's a "first" thing. There's also "the rest" of the list, which might or might not be empty…. i.e. a listDefinition by parts: first and rest. TeachScheme, ReachJava 2010

  9. One way to define lists ; A list is either empty or non-empty. ; Definition by choices: (define (function-on-list the-list)(cons [(empty? the-list) ; the-list an empty list ] [(non-empty? the-list) ; the-list a non-empty list ])) TeachScheme, ReachJava 2010

  10. One way to define lists ; An empty list has no parts at all. Definition by (no) parts. (define-struct empty []) ; make-empty: nothing -> empty ; empty? : object -> boolean ; A non-empty list has a first element and a rest which is itself a (possibly empty) list. Definition by parts. (define-struct non-empty [first rest]) ; make-non-empty : object list -> non-empty ; non-empty-first : non-empty -> object ; non-empty-rest : non-empty -> list ; non-empty? : object -> boolean TeachScheme, ReachJava 2010

  11. One way to define lists ; An empty list has no parts at all. Definition by parts, sorta. (define-struct empty []) … ; A non-empty list has a first element and a rest which is itself a (possibly empty) list. Definition by parts. (define-struct non-empty [first rest]) … "Examples of lists:" (make-empty) (make-non-empty 3 (make-empty)) (make-non-empty 4 (make-non-empty 3 (make-empty))) (make-non-empty "Joe" (make-non-empty "Mary" (make-empty))) TeachScheme, ReachJava 2010

  12. The way we really do it • This is a pain. • Lists are so common that Scheme provides predefined functions to handle them. • empty is a predefined constant, like true and false • cons is short for "construct": it constructs longer lists from shorter ones. (Equivalent to make-non-empty) ; cons : object list -> non-empty-list ; first : non-empty-list -> object ; rest : non-empty-list -> list ; empty? : object -> boolean ; cons? : object -> boolean TeachScheme, ReachJava 2010

  13. The way we really do it "Examples of lists:" empty (cons 3 empty) (cons 4 (cons 3 empty)) (cons "Joe" (cons "Mary" empty)) There is an even briefer notation, which we'll get to later. TeachScheme, ReachJava 2010

  14. Examples of working with lists (define list1 (cons "Joe" empty)) (define list2 (cons "Mary" list1)) (define list3 (cons "Bob" list2)) (define list4 (cons "Phil" (cons "Amy" empty))) (define list5 (cons 3 (cons 6 (cons -5 (cons 3 empty))))) (check-expect (empty? list1) false) (check-expect (cons? list1) true) (check-expect (first list1) "Joe") (check-expect (first list2) "Mary") (check-expect (first list3) "Bob") (check-expect (first list4) "Phil") (check-expect (first list5) 3) TeachScheme, ReachJava 2010

  15. Examples of working with lists (check-expect (rest list1) empty) (check-expect (rest list2) (cons "Joe" empty)) (check-expect (rest list3)(cons "Mary" (cons "Joe" empty))) (check-expect (rest list4) (cons "Amy" empty)) (check-expect (first (rest list4)) "Amy") (check-expect (first (rest (rest list5))) -5) TeachScheme, ReachJava 2010

  16. Template using choices and parts (define (function-on-list the-list) (cond [(empty? the-list) ; the-list an empty list ] [(cons? the-list) ; the-list a non-empty list ; (first the-list) whatever type is in the list ; (rest the-list) a list in its own right ])) TeachScheme, ReachJava 2010

  17. Template using choices and parts (define (function-on-list the-list) (cond [(empty? the-list) ; the-list an empty list ] [(cons? the-list) ; the-list a non-empty list ; (first the-list) whatever type is in the list ; (rest the-list) a list in its own right ; (function-on-list (rest the-list)) ])) TeachScheme, ReachJava 2010

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

  19. Shape of data -> shape of code! The data type was self-referential in the "rest" part of the "non-empty" case, so… the function is self-referential in the "rest" part of the "non-empty" case. Recursion has sneaked in; nothing new, just applying the coding patterns we already know. Students don't know recursion is hard unless you tell them. This form of recursion cannot go infinite! TeachScheme, ReachJava 2010

  20. Writing functions on lists ; count-elements : list -> number TeachScheme, ReachJava 2010

  21. Writing functions on lists ; count-elements : list -> number "Examples of count-elements:" (check-expect (count-elements empty) 0) (check-expect (count-elements (cons "Joe" empty)) 1) (check-expect (count-elements (cons "Phil" (cons "Joe" empty))) 2) TeachScheme, ReachJava 2010

  22. Writing functions on lists ; count-elements : list -> number (define (function-on-list the-list) (cond [(empty? the-list) ; the-list an empty list ] [(cons? the-list) ; the-list a non-empty list ; (first the-list) whatever type is in the list ; (rest the-list) a list in its own right ; (function-on-list (rest the-list)) ])) "Examples of count-elements:" (count-elements empty) "should be" 0 (count-elements (cons "Joe" empty)) "should be" 1 (count-elements (cons "Phil" (cons "Joe" empty))) "should be" 2 TeachScheme, ReachJava 2010

  23. Writing functions on lists ; count-elements : list -> number (define (count-elements the-list) (cond [(empty? the-list) ; the-list an empty list ] [(cons? the-list) ; the-list a non-empty list ; (first the-list) whatever kind of object is in the list ; (rest the-list) a list in its own right ; (count-elements (rest the-list)) a number ])) "Examples of count-elements:" … TeachScheme, ReachJava 2010

  24. Writing functions on lists ; count-elements : list -> number (define (count-elements the-list) (cond [(empty? the-list) ; the-list an empty list 0 ] ; from one of the test cases [(cons? the-list) ; the-list a non-empty list ; (first the-list) whatever kind of object is in the list ; (rest the-list) a list in its own right ; (count-elements (rest the-list)) a number ])) "Examples of count-elements:" … TeachScheme, ReachJava 2010

  25. Writing functions on lists ; count-elements : list -> number (define (count-elements the-list) (cond [(empty? the-list) ; the-list an empty list 0 ] [(cons? the-list) ; the-list a non-empty list ; (first the-list) whatever kind of object is in the list ; (rest the-list) a list in its own right (+ 1 (count-elements (rest the-list))) ])) "Examples of count-elements:" … TeachScheme, ReachJava 2010

  26. Now you try one ; add-up : list of numbers -> number "Examples of add-up:" (check-expect (add-up empty) 0) (check-expect (add-up (cons 3 empty)) 3) (check-expect (add-up (cons 4 (cons 3 empty))) 7) TeachScheme, ReachJava 2010

  27. Searching • We represent the ingredients on a pizza as a list of strings, e.g. • empty • (cons "cheese" empty) • (cons "garlic" (cons "cheese" empty)) • (cons "pepperoni" (cons "garlic" (cons "cheese" empty))) • We want to know whether there are any onions on the pizza. TeachScheme, ReachJava 2010

  28. Searching ; has-onions? : list-of-strings -> boolean "examples of has-onions?:" (check-expect (has-onions? empty) false) (check-expect (has-onions? (cons "cheese" empty)) false) (check-expect (has-onions? (cons "onions" empty)) true) (check-expect (has-onions? (cons "cheese" (cons "onions" (cons "pepperoni" empty)))) true) (check-expect (has-onions? (cons "cheese" (cons "garlic" (cons "pepperoni" empty)))) false) TeachScheme, ReachJava 2010

  29. Searching ; has-onions? : list-of-strings -> boolean (define (function-on-list the-list) (cond [(empty? the-list) ; the-list an empty list ] [(cons? the-list) ; the-list a non-empty list ; (first the-list) whatever type is in the list ; (rest the-list) a list in its own right ; (function-on-list (rest the-list)) ])) TeachScheme, ReachJava 2010

  30. Searching ; has-onions? : list-of-strings -> boolean (define (has-onions? ingredients) (cond [(empty? ingredients) ; ingredients an empty list ] [(cons? ingredients) ; ingredients a non-empty list of strings ; (first ingredients) a string ; (rest ingredients) a list of strings in its own right ; (has-onions? (rest ingredients)) a boolean ])) TeachScheme, ReachJava 2010

  31. Searching ; has-onions? : list-of-strings -> boolean (define (has-onions? ingredients) (cond [(empty? ingredients) ; ingredients an empty list false ; from one of the test cases ] [(cons? ingredients) ; ingredients a non-empty list of strings ; (first ingredients) a string ; (rest ingredients) a list of strings ; (has-onions? (rest ingredients)) a boolean ])) TeachScheme, ReachJava 2010

  32. Searching ; has-onions? : list-of-strings -> boolean (define (has-onions? ingredients) (cond [(empty? ingredients) ; ingredients an empty list false ; from one of the test cases ] [(cons? ingredients) ; ingredients a non-empty list of strings ; (first ingredients) a string ; (rest ingredients) a list of strings ; (has-onions? (rest ingredients)) boolean ; (string=? (first ingredients) "onions") boolean ])) TeachScheme, ReachJava 2010

  33. Searching ; has-onions? : list-of-strings -> boolean (define (has-onions? ingredients) (cond [(empty? ingredients) ; ingredients an empty list false ; from one of the test cases ] [(cons? ingredients) ; ingredients a non-empty list of strings ; (first ingredients) a string ; (rest ingredients) a list of strings ;(has-onions? (rest ingredients)) boolean ; (string=? (first ingredients) "onions") boolean(or (string=? (first ingredients) "onions") (has-onions? (rest ingredients))) ])) TeachScheme, ReachJava 2010

  34. A slightly different approach ; has-onions? : list-of-strings -> boolean (define (has-onions? ingredients) (cond [(empty? ingredients) ; ingredients an empty list false ; from one of the test cases ] [(cons? ingredients) ; ingredients a non-empty list of strings ; (first ingredients) a string ; (rest ingredients) a list of strings ;(has-onions? (rest ingredients)) boolean ; (string=? (first ingredients) "onions") boolean (cond [(string=? (first ingredients) "onions") true] [else (has-onions? (rest ingredients))]) ])) TeachScheme, ReachJava 2010

  35. Searching exercises Write a function any-over-100? which takes in a list of numbers and tells whether any of them are over 100 Write a function all-over-100? which takes in a list of numbers and tells whether all of them are over 100 Write a function string-in? which takes in a string and a list of strings and tells whether the string appears in the list TeachScheme, ReachJava 2010

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

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

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

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

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

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

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

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

  44. Are we done yet? • Fill out end-of-day survey • Eat • Go home • Sleep • Come back for another day

More Related