TeachScheme, ReachJava Adelphi University Tuesday afternoon June 23, 2009.

Slides:



Advertisements
Similar presentations
CSC 270 Nov. 22, 2005 Last Day of Scheme Dr. Stephen Bloch
Advertisements

Higher-Order Functions and Loops c. Kathi Fisler,
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Developing Programs for Family Trees c. Kathi Fisler, 2001.
Lists CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.1 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: AAA 1 ©
Developing Programs for Family Trees c. Kathi Fisler, 2001.
16-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
10 November JavaScript. Presentation Hints What do YOU think makes a good presentation Some of my suggestions Don’t write full sentences on slides Talk,
28-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
29-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
Ormap, andmap, and filter CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.3 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Recursion. Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example: A list.
Arrays. A problem with simple variables One variable holds one value –The value may change over time, but at any given time, a variable holds a single.
CSC 160 Computer Programming for Non-Majors Lecture #12: Syntax and Semantics Prof. Adam M. Wittenstein
Lists of Lists CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
Induction and recursion
Spelling Lists. Unit 1 Spelling List write family there yet would draw become grow try really ago almost always course less than words study then learned.
CS CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 5 Sept 4th, 2001 Fall Semester.
1 Python Control of Flow and Defining Classes LING 5200 Computational Corpus Linguistics Martha Palmer.
TeachScheme, ReachJava Adelphi University Tuesday morning July 13, 2010.
Arbitrarily Long Data Structures: Lists and Recursion CMSC Introduction to Computer Programming October 4, 2002.
Iteration. Adding CDs to Vic Stack In many of the programs you write, you would like to have a CD on the stack before the program runs. To do this, you.
TeachScheme, ReachJava Adelphi University Thursday morning June 25, 2009.
The Function Design Recipe CS 5010 Program Design Paradigms “Bootcamp” Lesson 1.1 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
TeachScheme, ReachJava Adelphi University Tuesday afternoon July 13, 2010.
Scheme: Compound Data Chapter 6 of HTDP Ms. Knudtzon September 19.
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
TeachScheme, ReachJava Adelphi University Wednesday morning June 24, 2008.
CSC 160 Computer Programming for Non-Majors Chapter 8: Scheme Language Review Prof. Adam M. Wittenstein
Generalizing Similar Functions CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.1 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
CS 330 Programming Languages 11 / 13 / 2008 Instructor: Michael Eckmann.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
TeachScheme, ReachJava Adelphi University Thursday morning July 15, 2010.
Rewriting your function using map and foldr CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual.
0 Odds and Ends in Haskell: Folding, I/O, and Functors Adapted from material by Miran Lipovaca.
TeachScheme, ReachJava Adelphi University Friday morning July 16, 2010.
Functional Programming Language 1 Scheme Language: part 2.
Lists vs. Structures CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative Commons.
Cs1321 December 6, 2001 Review. What is computer science? What's an algorithm? Processes and programs Overview of some programming language concepts Functional.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CS CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 13 October 4, 2001 Fall Semester.
Week 10 - Wednesday.  What did we talk about last time?  Method example  Roulette simulation  Types in Java.
Sometimes Structural Recursion Isn't Enough CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.1 TexPoint fonts used in EMF. Read the TexPoint manual.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
3-July-2002cse142-D2-Methods © 2002 University of Washington1 Methods CSE 142, Summer 2002 Computer Programming 1
Functional Programming Language 1 Scheme Language: part 3.
Generalizing Similar Functions
Examining Two Pieces of Data
More About Recursive Data Types
Class 11: Two-argument recursion
CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.3
CS 5010 Program Design Paradigms "Bootcamp" Lesson 9.3
Case Study: Undefined Variables
Recursion 12-Nov-18.
Lesson 2: Building Blocks of Programming
Recursion 2-Dec-18.
Recursion 2-Dec-18.
Recursion 29-Dec-18.
Assignment 7. Functional implementation of linked listed
Let's Play "What's the Question"
A First Program.
Recursion 23-Apr-19.
Classes, Objects and Methods
ormap, andmap, and filter
Developing Programs for Family Trees
Generalizing Similar Functions
Rewriting your function using map and foldr
Examining Two Pieces of Data
CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.1
Presentation transcript:

TeachScheme, ReachJava Adelphi University Tuesday afternoon June 23, 2009

Defining our own structs 1)Choose a name for the new data type 2)Choose names and types for each of its parts 3)Write a define-struct (next slide) to tell Scheme about it 4)Write contracts for the constructor and accessor methods 5)Write examples of the new data type 6)Write a function template for the new data type 7)Start writing functions on the new data type

Syntax Rule: Defining a Struct To define a structure named foo with fields snark and boojum, (define-struct foo (snark boojum)) This defines a new data type foo and several functions: ; make-foo : snark-type boojum-type -> foo ; foo-snark : foo -> snark-type ; foo-boojum : foo -> boojum-type ; foo? : anything -> boolean

Example: posn (if it weren't pre- defined) ; A posn consists of two numbers (x and y) (define-struct posn (x y)) ; make-posn : number number -> posn ; posn-x : posn -> number ; posn-y : posn -> number ; posn? : anything -> boolean "Examples of the posn data type:" (make-posn 3 5) (make-posn 12 -7) (define here (make-posn 14 9)) (check-expect (posn-x here) 14) (check-expect (posn-y here) 9)

Real example: dogs ; A dog has a string(name), number(age), number(weight), and a boolean(asleep?) (define-struct dog (name age weight asleep?)) ; make-dog : string num num boolean -> dog ; dog-name : dog -> string ; dog-age : dog -> number ; dog-weight : dog -> number ; dog-asleep? : dog -> boolean

Real example: dogs "Examples of the dog data type:" (make-dog "Ludo" 6 78 true) (make-dog "Thibaut" 4 74 false) (define this-dog (make-dog "Rover" 8 45 false)) (check-expect (dog-name this-dog) "Rover") (check-expect (dog-age this-dog) 8) (check-expect (dog-weight this-dog) 45) (check-expect (dog-asleep? this-dog) false)

Type-based coding patterns Almost any function that takes in a dog looks like (define (function-on-dog the-dog) ) (the "skeleton" step of the recipe)

Type-based coding patterns Almost any function that takes in a dog looks like (define (function-on-dog the-dog) ; the-dog a dog ) (the "inventory" step of the recipe)

Type-based coding patterns Almost any function that takes in a dog looks like (define (function-on-dog the-dog) ; the-dog a dog ; (dog-name the-dog) a string ; (dog-age the-dog) a number ; (dog-weight the-dog) a number ; (dog-asleep? the-dog) a boolean ) (the "inventory" step of the recipe, continued)

Type-based coding patterns #| (define (function-on-dog the-dog) ; the-dog a dog ; (dog-name the-dog) a string ; (dog-age the-dog) a number ; (dog-weight the-dog) a number ; (dog-asleep? the-dog) a boolean ) |# This header-plus-inventory, commented out, can be used as a template, copying and pasting it as a starting point for any function that takes in a dog.

A function on dogs Write a function fits-in-lap? which takes in a dog and the weight capacity of a lap, and tells whether the dog will fit in the lap in question Work this out together

Your turn Write a function movable? which takes in a dog. If the dog is awake, it's movable. If the dog is asleep but under 20 pounds, it's movable. Otherwise it's not. (Hint: it's shorter and simpler if you don't use a cond.) Write a function birthday which takes in a dog and returns a dog with the same name, weight, and sleep status, but one year older.

Inventors and Factories define-struct is like an inventor. Specifies once what's in a particular model of cell phone, but doesn't actually manufacture them. Then invents something else, and doesn't manufacture them either…. make-posn is like a cell-phone factory. Each factory "knows" how to build one kind of thing (cell phones, posns, dogs, etc.) Factory doesn't exist until the thing is invented. Can be used over and over to build many cell- phones/posns/dogs/whatever.

Defining another animal Define a data structure fish which has a string(color), a number(weight) and a boolean(salt-water?). Remember the steps: 1)Choose a name for the new data type  2)Choose names and types for each of its parts  3)Write a define-struct to tell Scheme about it 4)Write contracts for the constructor and accessor methods 5)Write examples of the new data type 6)Write a function template for the new data type 7)Start writing functions on the new data type

Definition by choices New data type: animal ; An animal is either a dog or a fish. Scheme doesn't enforce this; it's up to the programmer.

A function on animals Write a function fits-in-crate? which takes in an animal (either a dog or a fish) and the weight capacity of a crate, and tells whether the animal can be shipped in that crate. Hint: fish are never shipped in crates, regardless of weight.

How to write this? The input type, animal, is one of two sub-categories (dog or fish), so… we need at least two test cases, one of each type (in fact, we'll need three dogs — under, over, and borderline — and at least one fish) the function body will probably be a cond with two cases, with questions "dog?" and "fish?"

Function template for animals Almost any function on animals will look like (define (function-on-animal the-animal) )

Function template for animals Almost any function on animals will look like (define (function-on-animal the-animal) (cond [(dog? the-animal) ] [(fish? the-animal) ]))

Function template for animals Almost any function on animals will look like (define (function-on-animal the-animal) (cond [(dog? the-animal) ; the-animal a dog ] [(fish? the-animal) ; the-animal a fish ]))

Function template for animals #| (define (function-on-animal the-animal) (cond [(dog? the-animal) ; the-animal a dog ; (dog-name the-animal) a string ; (dog-age the-animal) a number ; (dog-weight the-animal) a number ; (dog-asleep? the-animal) a boolean ] [(fish? the-animal) ; the-animal a fish ; (fish-color the-animal) a string ; (fish-weight the-animal) a number ; (fish-salt-water? the-animal) a boolean ])) |#

Function template for animals Again, we can copy and paste this as a starting point for any function on animals. In practice, much of it is irrelevant to any given function, so we can delete those lines.

My answer to fits-in-crate? ; fits-in-crate? : animal number -> boolean (define (fits-in-crate? the-animal max-weight) ; max-weight a number (cond [(dog? the-animal) ; (dog-weight the-animal) a number (<= (dog-weight the-animal) max-weight) ] [(fish? the-animal) false ])) "Examples of fits-in-crate?:" (check-expect (fits-in-crate? (make-dog "Bob" 3 58 true) 50) false) (check-expect (fits-in-crate? (make-dog "Dave" 2 65 true) 65) true) ; borderline (check-expect (fits-in-crate? (make-dog "Eddie" 7 35 false) 50) true) (check-expect (fits-in-crate? (make-fish "orange" 0.03 false) 5) false)

Another exercise Write a function underweight? which takes in an animal and tells whether or not it's underweight — which means under 30 pounds for a dog, and under 0.1 pounds for a fish.

Lab exercise Open shapes-lab.scm (in Examples folder) Do the exercises in it

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, …

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 )

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

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?

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

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 list Definition by parts: first and rest.

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

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

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

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

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.

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)

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)

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

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

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!

Writing functions on lists ; count-elements : list -> number

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)

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

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:" …

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:" …

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:" …

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)

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.

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)

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

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

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

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

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

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

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

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