Class 6: Programming with Data David Evans cs1120 Fall 2009.

Slides:



Advertisements
Similar presentations
TWO STEP EQUATIONS 1. SOLVE FOR X 2. DO THE ADDITION STEP FIRST
Advertisements

You have been given a mission and a code. Use the code to complete the mission and you will save the world from obliteration…
Advanced Piloting Cruise Plot.
Chapter 1 The Study of Body Function Image PowerPoint
Copyright © 2011, Elsevier Inc. All rights reserved. Chapter 5 Author: Julia Richards and R. Scott Hawley.
1 Copyright © 2010, Elsevier Inc. All rights Reserved Fig 2.1 Chapter 2.
By D. Fisher Geometric Transformations. Reflection, Rotation, or Translation 1.
Business Transaction Management Software for Application Coordination 1 Business Processes and Coordination.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Title Subtitle.
My Alphabet Book abcdefghijklm nopqrstuvwxyz.
0 - 0.
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
SUBTRACTING INTEGERS 1. CHANGE THE SUBTRACTION SIGN TO ADDITION
MULT. INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
FACTORING ax2 + bx + c Think “unfoil” Work down, Show all steps.
Addition Facts
Year 6 mental test 5 second questions
Around the World AdditionSubtraction MultiplicationDivision AdditionSubtraction MultiplicationDivision.
ZMQS ZMQS
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append.
BT Wholesale October Creating your own telephone network WHOLESALE CALLS LINE ASSOCIATED.
Report Card P Only 4 files are exported in SAMS, but there are at least 7 tables could be exported in WebSAMS. Report Card P contains 4 functions: Extract,
Cs1120 Fall 2009 David Evans Lecture 9: Mostly Not About Matter, Music, and Mayhem Richard Feynmans van Alan Alda playing.
Randomized Algorithms Randomized Algorithms CS648 1.
Plt /7/ Data Abstraction Programming Language Essentials 2nd edition Chapter 2.2 An Abstraction for Inductive Data Types.
Data Structures Using C++
Modern Programming Languages, 2nd ed.
ABC Technology Project
Day of the week 9:00 – 10:00 10: :00 12: :00 13: :00 14: :00 15: :00 16:00 – 17:00 17: :00 18:00 – 19:00 19:00 – 20:00.
Class 3: Rules of Evaluation David Evans cs1120 Fall 2009.
© Glenn Rowe AC Lab 2 A simple card game (twenty- one)
Cs1120 Fall 2009 David Evans Lecture 11: Generalizing List Procedures 1.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 6: Cons car cdr sdr wdr.
1 Undirected Breadth First Search F A BCG DE H 2 F A BCG DE H Queue: A get Undiscovered Fringe Finished Active 0 distance from A visit(A)
VOORBLAD.
1 Breadth First Search s s Undiscovered Discovered Finished Queue: s Top of queue 2 1 Shortest path from s.
A Strong Church. Introduction Our goal should be to help our congregation become a strong church. We better appreciate the need for a strong church when.
Squares and Square Root WALK. Solve each problem REVIEW:
© 2012 National Heart Foundation of Australia. Slide 2.
Lets play bingo!!. Calculate: MEAN Calculate: MEDIAN
1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010.
C-LISP. LISP 2 Lisp was invented by John McCarthy in 1958 while he was at the Massachusetts Institute of Technology (MIT).John McCarthyMassachusetts Institute.
Chapter 5 Test Review Sections 5-1 through 5-4.
SIMOCODE-DP Software.
GG Consulting, LLC I-SUITE. Source: TEA SHARS Frequently asked questions 2.
1 First EMRAS II Technical Meeting IAEA Headquarters, Vienna, 19–23 January 2009.
Addition 1’s to 20.
25 seconds left…...
Januar MDMDFSSMDMDFSSS
Week 1.
We will resume in: 25 Minutes.
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
A SMALL TRUTH TO MAKE LIFE 100%
1 Unit 1 Kinematics Chapter 1 Day
PSSA Preparation.
How Cells Obtain Energy from Food
Introduction to Recursion and Recursive Algorithms
Index Student Activity 1: Questions to familiarise students with the
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Class 8: Recursing on Lists David Evans cs1120 Fall 2009.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 5: Recursing on Lists.
Class 7: List Procedures David Evans cs1120 Fall 2009.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 4: Programming with Data.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 8: Cons car cdr sdr wdr.
Lecture 6: Programming with Data CS150: Computer Science
Presentation transcript:

Class 6: Programming with Data David Evans cs1120 Fall 2009

Ways to Design Programs 1.Think about what you want to do, and turn that into code. 2.Think about what you need to represent, and design your code around that. Which is better? 2

History of Scheme Scheme [Guy Steele & Gerry Sussman, 1975] Guy Steele co-designed Scheme and created the first Scheme interpreter for his 4 th year project More recently, Steele specified Java [1995] – Conniver [1973] and Planner [1967] Based on LISP [John McCarthy, 1958] – Based on Lambda Calculus [Alonzo Church, 1930s] – Last few lectures in course 3

LISP Lots of Insipid Silly Parentheses LISt Processing language Lists are pretty important – hard to write a useful Scheme program without them. 4

Making Lists 5

Making a Pair > (cons 1 2) (1. 2) cons constructs a pair 12 6

Splitting a Pair > (car (cons 1 2)) 1 > (cdr (cons 1 2)) 2 car extracts first part of a pair cdr extracts second part of a pair 12 car cdr 7

Pair Examples 8 > (cons (cons 1 2) 3)) ((1. 2) 3) > (cdr (car (cons (cons 1 2) 3))) 2 > (car (car (cons 1 (cons 2 3)))) car: expects argument of type ; given 1

Why car and cdr? Original (1950s) LISP on IBM 704 – Stored cons pairs in memory registers – car = Contents of the Address part of the Register – cdr = Contents of the Decrement part of the Register (could-er) Doesnt matter unless you have an IBM 704 Think of them as first and rest (define first car) (define rest cdr) (The DrScheme Pretty Big language already defines these, but they are not part of standard Scheme) 9

Implementing cons, car and cdr (define (cons a b) (lambda (w) (if w a b))) (define (car pair) (pair #t) (define (cdr pair) (pair #f) Scheme provides primitive implementations for cons, car, and cdr. But, we could define them ourselves. 10

Pairs are fine, but how do we make threesomes? 11

Triple A triple is just a pair where one of the parts is a pair! (define (triple a b c) (cons a (cons b c))) (define (t-first t) (car t)) (define (t-second t) (car (cdr t))) (define (t-third t) (cdr (cdr t))) 12

Quadruple A quadruple is a pair where the second part is a triple (define (quadruple a b c d) (cons a (triple b c d))) (define (q-first q) (car q)) (define (q-second q) (t-first (cdr t))) (define (q-third t) (t-second (cdr t))) (define (q-fourth t) (t-third (cdr t))) 13

Multuples A quintuple is a pair where the second part is a quadruple A sextuple is a pair where the second part is a quintuple A septuple is a pair where the second part is a sextuple An octuple is group of octupi A ? is a pair where the second part is a …? 14

Lists List ::= (cons Element List) A list is a pair where the second part is a list. One big problem: how do we stop? This only allows infinitely long lists! 15

Lists List ::= (cons Element List) List ::= A list is either: a pair where the second part is a list or, empty Its hard to write this! 16

Null List ::= (cons Element List) List ::= A list is either: a pair where the second part is a list or, empty (null) null 17

Returning Problem Sets Problem Input: unordered set of cs1120 students Output: cs1120 students in lexicographic order by UVa ID 18 What is a good algorithm for getting all of you in order by UVa ID?

Recap A List is either: a Pair where the second part is a List or null Pair primitives: (cons a b)Construct a pair (car pair)First part of a pair (cdr pair)Second part of a pair 19

List Examples > null () > (cons 1 null) (1) > (list? null) #t > (list? (cons 1 2)) #f > (list? (cons 1 null)) #t 20

More List Examples > (list? (cons 1 (cons 2 null))) #t > (car (cons 1 (cons 2 null))) 1 > (cdr (cons 1 (cons 2 null))) (2) 21

Problem Set 2: Programming with Data Representing a card car cdr Pair of rank (Ace) and suit (Spades) 22

Problem Set 2: Programming with Data Representing a card: (define (make-card rank suit) (cons rank suit)) Representing a hand (list (make-card Ace clubs) (make-card King clubs) (make-card Queen clubs) (make-card Jack clubs) (make-card 10 clubs)) 23

list-trues Define a procedure that takes as input a list, and produces as output the number of non- false values in the list. (list-trues null) 0 (list-trues (list 1 2 3)) 3 (list-trues (list false (list 2 3 4))) 1 24

Charge Now, you know everything you need for Problem Set 2 (and PS3 and PS4) – Help hours Sunday, Monday, Tuesday, Wednesday Probably a Quiz Wednesday – Course book through Sec. 5.4 and GEB reading – PS1 Comments Class Wednesday and Friday: – Lots of examples programming with procedures and recursive definitions 25