Scheme: Lists Cont’d Chapter 9-10 of HTDP Ms. Knudtzon September 26.

Slides:



Advertisements
Similar presentations
Chapter 7. Binary Search Trees
Advertisements

1 Functional Programming Lecture 8 - Binary Search Trees.
Higher-Order Functions and Loops c. Kathi Fisler,
Lisp. Versions of LISP Lisp is an old language with many variants Lisp is alive and well today Most modern versions are based on Common Lisp LispWorks.
Scheme: Functions Chapter 3 of HTDP Ms. Knudtzon September 12.
Binary Trees. DCS – SWC 2 Binary Trees Sets and Maps in Java are also available in tree-based implementations A Tree is – in this context – a data structure.
Recursion, pt. 2: Thinking it Through. What is Recursion? Recursion is the idea of solving a problem in terms of solving a smaller instance of the same.
Searching Kruse and Ryba Ch and 9.6. Problem: Search We are given a list of records. Each record has an associated key. Give efficient algorithm.
Multi-way Trees CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.6 © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
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 ©
CSE 143 Lecture 17 Binary Search Trees and Comparable slides created by Ethan Apter
Developing Programs for Family Trees c. Kathi Fisler, 2001.
Lisp. Versions of LISP Lisp is an old language with many variants –LISP is an acronym for List Processing language Lisp is alive and well today Most modern.
Data Structures Data Structures Topic #8. Today’s Agenda Continue Discussing Table Abstractions But, this time, let’s talk about them in terms of new.
This material in not in your text (except as exercises) Sequence Comparisons –Problems in molecular biology involve finding the minimum number of edit.
28-Jun-15 Recognizers. 2 Parsers and recognizers Given a grammar (say, in BNF) and a string, A recognizer will tell whether the string belongs to the.
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Searching via Traversals Searching a Binary Search Tree (BST) Binary Search on a Sorted Array Data Structure Conversion and Helper Modules.
From Templates to Folds CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative Commons.
Lecture for Week Spring.  Numbers can be represented in many ways. We are familiar with the decimal system since it is most widely used in everyday.
Tree.
Arbitrarily Long Data Structures: Lists and Recursion CMSC Introduction to Computer Programming October 4, 2002.
CS CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 11 Sept 27th, 2001 Fall Semester.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
Scheme: Compound Data Chapter 6 of HTDP Ms. Knudtzon September 19.
B-Trees. CSM B-Trees 2 Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so.
Priority Queues and Binary Heaps Chapter Trees Some animals are more equal than others A queue is a FIFO data structure the first element.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
B-Trees. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it.
Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 8: Accumulating.
CS CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 10 Sept 25th, 2001 Fall Semester.
Data Structures & Algorithms
Data Abstraction: Sets Binary Search Trees CMSC Introduction to Computer Programming October 30, 2002.
Functional Programming Language 1 Scheme Language: part 2.
Cs1321 December 6, 2001 Review. What is computer science? What's an algorithm? Processes and programs Overview of some programming language concepts Functional.
1/32 This Lecture Substitution model An example using the substitution model Designing recursive procedures Designing iterative procedures Proving that.
Binary Search Trees (BST)
Tree Data Structures. Heaps for searching Search in a heap? Search in a heap? Would have to look at root Would have to look at root If search item smaller.
B-TREE. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it won’t.
2014-T2 Lecture 27 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas.
CS CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 13 October 4, 2001 Fall Semester.
Sometimes Structural Recursion Isn't Enough CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.1 TexPoint fonts used in EMF. Read the TexPoint manual.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
1 Chapter 4 Unordered List. 2 Learning Objectives ● Describe the properties of an unordered list. ● Study sequential search and analyze its worst- case.
Chapter 7 Trees_ Part2 TREES. Depth and Height 2  Let v be a node of a tree T. The depth of v is the number of ancestors of v, excluding v itself. 
Keeping Binary Trees Sorted. Search trees Searching a binary tree is easy; it’s just a preorder traversal public BinaryTree findNode(BinaryTree node,
11 Making Decisions in a Program Session 2.3. Session Overview  Introduce the idea of an algorithm  Show how a program can make logical decisions based.
Data Abstraction: Sets
CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.2
CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.3
Binary Search Tree (BST)
This Lecture Substitution model
Hash Tables Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common.
CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.5
Let's Play "What's the Question"
Hash Tables Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common.
CSC 143 Java Trees.
This Lecture Substitution model
Developing Programs for Family Trees
B-Trees.
B-Trees.
Lisp.
Bringing it all Together: Family Trees
Presentation transcript:

Scheme: Lists Cont’d Chapter 9-10 of HTDP Ms. Knudtzon September 26

Producing Lists in a Function Remember the wage function we wrote: (define hour-rate 12) (define (wage h) (* hour-rate h)) What if we wanted to extend the function - a company really wants us to compute the wages for all its employees We want to input the list of the hours each employee worked and output the list of the wages for each employee

List Template ;; hours-->wages: list-of-numbers --> lists-of-numbers ;; to create a list of weekly wages from a list of weekly hours (alon) (define (hours-->wages alon) (cond [ (empty? alon) … ] [ else … (first alon) … … (hours-->wages (rest alon)) … ] )) But how do we fill in this template to return a list? Could also do (cons? alon) here

hours-->wages We want to compute the wage for the first item in the list of weekly hours  So we will probably want to use our wage function that computes the wage for one set of hours worked Then we want to construct a list that represents the wages for alon, using the first weekly wage that we’ve computed and the list of weekly wages for (rest alon)  The (rest alon) in the template reminds us that the function we write can process this list recursively Any ideas on how to do this?

Completed: hours-->wages ;; hours-->wages: list-of-numbers --> listsof-numbers ;; to create a list of weekly wages from a list of weekly hours (alon) (define (hours-->wages alon) (cond [ (empty? alon) empty ] [ else (cons (wage (first alon)) (hours-->wages (rest alon)) ] ))

Practice! Exercise Develop the function convert- euro, which converts a list of U.S. dollar amounts into a list of euro amounts. Assume the exchange rate is 1.22 euro for each dollar. Generalize convert-euro to the function convert- euro-1, which consumes an exchange rate and a list of dollar amounts and converts the latter into a list of euro amounts.

Adding even more complexity… So far we have dealt with lists of simple data types, but in the real world, we could have lists with structures (instead of just atomic values) Maybe we want to keep track of an inventory for a store in which they want to keep track of all the items and the item’s price

An Inventory Record (define-struct ir (name price) ;; An inventory-record (ir) is a structure: ;(make-ir symbol number) (define ir1 (make-ir ‘robot 500)) Don’t forget to write the template for the ir: ;; ir-func: ir --> ?? ;; purpose: (define (ir-func record) … (ir-name record)… … (ir-price record)…)

An inventory An inventory would then be a list of irs  So use the ir structure as the basis for our list: ;; An inventory is either ;; empty or ;; (cons ir inv), where ir is an inventory record & inv is an inventory Note: this data definition actually has two arrows One back to the definition of ir One back to the definition of itself (inventory)

Inventory Template ;; inv-func: inventory --> ??? ;; Purpose: Consumes an inventory (list of irs) and …. (define (inv-func an-inv) (cond [ (empty? an-inv) …] [ else … (ir-func (first an-inv)) … … (inv-func (rest an-inv)) … ]))

Extract Function Define the extract1 function, which takes an-inv and removes anything that costs less than $1 ;; extract1: inventory --> inventory (define (extract an-inv) (cond [ (empty? an-inv) empty] [ else (cond [ (<= (ir-price (first an-inv)) 1.00) (cons (first an-inv) (extract1 (rest an-inv)))] [else (extract1 (rest an-inv)) ] ) ])) Here we just pull out the piece because it’s just one item

Exercise: convert-euro Develop the function convert-euro, which converts a list of U.S. dollar amounts into a list of euro amounts. Assume the exchange rate is 1.22 euro for each dollar. Generalize convert-euro to the function convert- euro-1, which consumes an exchange rate and a list of dollar amounts and converts the latter into a list of euro amounts.

Exercise: Something Fun Provide a data definition and a structure definition for an inventory that includes pictures with each inventory record. Develop the function show-picture. The function consumes a symbol, the name of a toy, and one of the new inventories. It produces the picture of the named toy or false if the desired item is not in the inventory You can define images and use them in your program by using: Special > Insert Image

List shortcut (list 1 2 3) will make: (cons 1 (cons 2 (cons 3 empty))) (list (make-posn 3 4) (make-posn 2 3) (make- posn 5 6)) will do: (cons (make-posn 3 4) (cons (make-posn 2 3) (cons (make-posn 5 6) empty))) You can use this for making lists, but remember, Scheme still constructs the list with cons and that is what you will need for your data definitions and for your templates

List Project The person responsible for counting the votes in a recent Washington DC primary election needs some help. Given the recent problems with vote counting across the nation, he is concerned that the old system might cause controversy. As an expert programmer, you have been asked to help develop a new system that will correctly count the votes in a recent election, as well as providing some statistics about the election. Unfortunately, the person you are working for is very busy, and has only provided you with some raw data and told you what you are expected to program. It is your job to figure out a way to finish the tasks before the deadline next Thursday.

Scheme: Composing Functions, More on Recursion Chapter 12 of HTDP Ms. Knudtzon September 27

Auxiliary Functions We talked about defining auxiliary functions for every dependency between quantities in the problem statement.  Today we are going to look at other situations in which we may want to design auxiliary functions.

Designing Complex Functions When we analyze a problem statement, we may find:  That the formulation of an answer requires different cases of values Use a cond expression  That the computation requires knowledge of a particular domain Us an auxiliary function  That a computation must process a list or some other data of arbitrary size Use an auxiliary function  That we need a specific kind of answer Maybe the general case could be placed in an auxiliary function

“Wish Lists” Often we end up with a problem that may require several auxiliary functions  We should keep a list of functions (with contract, header & purpose) that need to be developed in order to complete the function Once you have a list of functions that need to be completed, you can see if there might be something existing in Scheme already, or decide which needs to be implemented next, depending on what information we already have  The (extra-credit) winning judge part of the List Project requires this

Recursive Auxiliary Function: Sorting Write a program that takes a list of numbers and sorts it, returning the new, sorted list of numbers ;; sort: list-of-numbers --> list-of-numbers ;; to create a sorted list of numbers from all the ;; numbers in alon (define (sort alon) ….)

Function template for Sort Examples of data? (sort empty) “should be” …… (sort (cons 324 (cons 634 (cons -343 empty)))) “should be” ……. ;; sort: list-of-numbers --> list-of-numbers ;; to create a sorted list of numbers from all the ;; numbers in alon (define (sort alon) (cond [ (empty? alon) …] [ else … (first alon) … (sort (rest alon)) …]))

What next? For the empty case, the answer is empty, as seen in the example. What do we want to happen in the else case?  We want to put together the two pieces  (first alon) extracts the first number  (sort (rest alon)) produces a sorted version of (rest alon) So we want to insert the first number into the correct place in the sorted list

Insert We know we want to insert (first alon) into the sorted rest of the list  But how do we find the right place to insert our number? -- We have to find the proper place in the list An auxiliary function that will handle this task is a good idea here! ;;insert: number list-of-numbers --> list-of-numbers ;; to create a list of numbers from n and the list alon ;; that is sorted in descending order; alon is already sorted (define (insert n alon) … )

Sort So with that auxiliary function, we could finish our sort definition: [else (insert (first alon) (sort (rest alon))) ] But we still need to develop the insert auxiliary function

insert Sample cases to figure out what is happening Template of insert function: (cond [ (empty? alon) …] [else … (first alon) … (insert n (rest alon)) …] What do we want to happen in each case?

Insert cases Empty case: (cons n empty) What about when alon is not empty?  Consider examples: (insert 7 (cons 6 (cons 5 (cons 4 empty)))) (insert 3 (cons 6 (cons 1 (cons -1 empty)))) (insert 3 (cons 6 (cons 3 (cons -1 empty)))) --> How many new cases do we have?

Cases (cond [ (>= n (first alon)) …] [(< n (first alon)) …]) So what do we need to do in each case?

Insert Function ;;insert: number list-of-numbers --> list-of-numbers ;; to create a list of numbers from n and the list alon ;; that is sorted in descending order; alon is already sorted (define (insert n alon) (cond [ (empty? alon) (cons n empty)] [else (cond [ (>= n (first alon)) (cons n alon)] [ else (cons (first alon) (insert n (rest alon)))]) ]))

Scheme: Exercises Ms. Knudtzon September 28

Exercises Exercise Develop a program that sorts lists of mail messages by name. Mail structures are defined as follows: (define-struct mail (from date message)) ;; A mail-message is a structure: ;;(mak name n s) ;; where name is a string, n is a number, and s is a string. To compare two strings alphabetically, use the string<? primitive

Exercises Exercise Here is the function search: ;; search : number list-of-numbers-->boolean (define (search n alon) (cond [(empty? alon) false] [else (or (= (first alon) n) (search n (rest alon)))])) It determines whether some number occurs in a list of numbers. The function may have to traverse the entire list to find out that the number of interest isn't contained in the list. Develop the function search-sorted, which determines whether a number occurs in a sorted list of numbers. The function must take advantage of the fact that the list is sorted.

Scheme: Trees Chapter 14 of HTDP Ms. Knudtzon September 29

Family Trees Let’s sketch out how we would design a family tree  First, let’s actually sketch a sample family tree.  What are the major components?  What structures do we need?  How do we do the data definition for the tree?

The data (define-struct person (name year eye-color mother father)) ;;A person is ;;(make-person string number symbol person person) ;; ;; Note: [[circles back to make-person]] But can we actually create data from the definition?  We have self-referential data, but no additional “stopping” clauses (like empty for lists)

Child Node While we could use empty, let’s introduce a new symbol ‘unknown and make a new date definition ;; A child node is a ;;(make-person name year eye-color mom dad) ;; where mom and dad are ‘unknown or child nodes ;; name is a string, eye-color is a symbol, year is a number But now we have two alternatives for the last two components … which is no good

Family Tree Node (ftn) Instead, let’s define the collection of nodes in the tree ;; A family-tree-node (ftn) is either ;; - ‘unknown or ;;(make-person name year eye-color mom dad) ;; where mom and dad are family-tree-nodes ;; name is a string, eye-color is a symbol, year is a number Now we can define an example that works

Tree Example (define tree1 (make-person “Mary” 1985 ‘blue (make-person “Anna” 1960 ‘green ’unknown (make-person “Hal” 1930 ‘blue ‘unknown ‘unknown) ) (make-person “Joe” 1958 ‘green ‘unknown ‘unknown))) Or Better to see the information: ;; Oldest generation (define Hal (make-person “Hal” 1930 ‘blue ‘unknown ‘unknown) ;; middle generation (define Anna (make-person “Anna” 1960 ‘green ’unknown Hal) (define Joe (make-person “Joe” 1958 ‘green ‘unknown ‘unknown))) ;;youngest generation (define mary (make-person “Mary” 1985 ‘blue Anna Joe) (define tree1 mary)

Template for FTN Now that we have the data definition for a family tree node (ftn) let’s make the template: ;; Contract: ft-func: family-tree --> xxx ;; Purpose: (define (ft-func aft) (cond [(symbol=? ‘unknown aft) xxx]

Template ;; Contract: ft-func: family-tree --> xxx ;; Purpose: (define (ft-func aft) (cond [(symbol? aft) xxx] [(person? aft) (person-name aft) (person-year aft) (person-eye-color aft) (ft-func (person-mother aft)) ;;do arrows (ft-func (person-father aft))] ) ) ;;do arrows ;; This says any symbol is a terminator ;; generic equality checking operator might be better: (eq? ‘unknown aft) ;; to enforce unknown

is-in? (Using template) ;; Contract: is-in?: string family-tree --> boolean ;; Purpose: To determine wheter a given name appears in family tree ;; Tests (is-in? “Anna” ‘unknown) “should be” false (is-in? “Anna” tree1) “should be” true (is-in? “Fred” tree1) “should be” false (define (is-in? name aft) (cond [(symbol? aft) false] ;; remember, this is any symbol [(person? aft) (or (string=? name (person-name aft) (is-in? (person-mother aft)) ;;do arrows (is-in? (person-father aft))) ] ) ) ;;do arrows

Count function ;; count: family-tree --> number ;; produce number of people in the family tree (define (count aft) (cond [(symbol? aft) 0] [(person? aft) (+ 1

Count-ww2-babies ;; count-ww2-babies: family-tree --> number ;; produce number of people in tree born between inclusive define (count-ww2-babies aft) (cond [(symbol? aft) 0] [(person? aft) (+ (ww2-baby (person-year aft)) (count-ww2-babies (person-mother aft)) (count-ww2-babies(person-father aft))) ] ) ) ;; helper ww2-baby: number --> number ;; Return 0 if not, 1 is is (define (ww2-baby? year) (cond [ (<= 1941 year 1945) 1] [ else 0] ))

Blue-eyed-ancestor? Write a function that determines whether a family tree contains a child structure with ‘blue in the eyes field

Average-age Develop the function average-age which consumes a family tree and the current year. It produces the average age of all people in the family tree.

Scheme: Binary Trees Ms. Knudtzon September 29

Checkpoint How did last night’s exercise go?  Count-ww2-babies  Average-age  blueEyedAncestor Let’s look at the solutions together

Binary Tree Node We are going to define a new kind of tree that contains nodes: (define-struct node (data left right)) ;; A node is a ;; (make-node d l r) ;; where d is a number, and l&r are nodes or empty)

Binary Trees A binary tree is one in which every node has one left and one right child node (or empty to indicate lack of node): A binary-tree (BT) is either: - empty (make-node number BT BT)

Binary Search Tree A binary search tree (bst) is a special kind of binary tree that manages information in a way that makes it easy to store and retrieve  The items in the left tree are all less than the node  The items in the right tree are all greater than or equal to the node

Trees (Pictures) Binary Tree Binary Search Tree

BST Data Definition A binary-search-tree (BST) is a BT: -- empty is always a BST -- (make-node d lft rgt) is a BST if: -- lft and rgt are BSTs -- all data numbers in lft are smaller than d -- all data numbers in rgt are larger or equal to d NOTE: we have a different kind of restriction on how to construct BSTs -- must inspect numbers in trees and compare them to the node at hand

Building Trees Building binary trees is easy Building a binary-search-tree is complicated and error prone  We can’t just create two trees and a new node and join them (like we can with a binary tree)  So instead, we can either: Design the tree by hand after getting the list of data and then make the corresponding tree with make-node Write a function that builds a BST from a list, one node after another

Exercise Exercise Develop the function inorder. It consumes a binary tree and produces a list of all the data numbers in the tree. The list contains the numbers in the left-to-right order we have used above. Hint: Use the Scheme operation append, which concatenates lists:( append (list 1 2 3) (list 4) (list 5 6 7)) evaluates to (list ) What does inorder produce for a binary search tree?