Class 6: Programming with Data
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Class 6: Programming with Data

on

  • 514 views

Procedure practice: middle, find-fixedpoint

Procedure practice: middle, find-fixedpoint
History of Scheme
Introducing Pairs

Statistics

Views

Total Views
514
Views on SlideShare
514
Embed Views
0

Actions

Likes
0
Downloads
6
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • (define middle (lambda (a b c) (if (< a b) (if (< b c) b (if (< a c) c a)) (if (< b c) (if (< a c) a c) b))))
  • (define find-fixedpoint (lambda (f x) (if (= (f x) x) x (find-fixedpoint f (f x)))))(require racket/trace)(define (mycos x) (cos x))(trace mycos)(find-fixedpointmycos 0)

Class 6: Programming with Data Presentation Transcript

  • 1. Class 6:Programmingwith Datacs1120 Fall 2011David Evans5 September 2011
  • 2. PlanPractice Procedures from FridayIntroducing Lists
  • 3. Practice Procedure: identity1. Define a procedure, identity, that takes one input, and outputs that value.
  • 4. Practice Procedure: pick-oneDefine a procedure, pick-one, that takes three inputs. Thefirst input is a Boolean value (either true or false). If the valueof the first input is true, the output is the value of the secondinput; if the value of the first input is false, the output is thevalue of the third input.
  • 5. Practice Procedure: middleDefine a procedure, middle, that takes three numbers asinputs, and outputs the number that is in the middle. abc
  • 6. a<b yes b<c b<c yes no yes nob a<c a<c b yes no yes no c a a c
  • 7. Binary Decision Diagram Cartoon by Geoff Draper
  • 8. a<b yes (define middle b<c b<c (lambda (a b c) yes no yes nob a<c a<c b yes no yes no c a a c
  • 9. Is there a better middle procedure?
  • 10. A Better Middle?(define middle (lambda (a b c) (min (max a b) (max b c) (max a c)))) Suggested by Peter Chapman
  • 11. Code Golf http://codegolf.com/
  • 12. Size matters…but isn’t everything: speed, clarity, memory use, etc.
  • 13. Practice Procedure: find-fixedpointDefine a procedure, find-fixedpoint, that takesas input a function and an initial value, andoutputs the fixed point of the function startingfrom that value. A fixed point of a function f is avalue x such that (f x) evaluates to x.
  • 14. Ways to Design Programs1. 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? 14
  • 15. History of SchemeGuy Steele co-designedScheme, with GerrySussman, and createdthe first Schemeinterpreter for his 4thyear project (1975)More recently, Steelespecified Java [1995] Guy Steele 15
  • 16. Pre-History of SchemeScheme [1975]Conniver [1973]Planner [1967]Based on LISP [John McCarthy, 1958]Based on Lambda Calculus [Alonzo Church, 1930s] John McCarthy
  • 17. LISP“Lots of Insipid Silly Parentheses”“LISt Processing language” Lists are really important – hard to write a useful Scheme program without them. 17
  • 18. Making ListsFreedom Park, Tshwane/Pretoria, South Africa 18
  • 19. Making a Pair> (cons 1 2)(1 . 2) 1 2 cons constructs a pair 19
  • 20. Splitting a Pair car cdr> (car (cons 1 2))1> (cdr (cons 1 2))2 1 2car extracts first part of a paircdr extracts second part of a pair 20
  • 21. Pair Examples> (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 <pair>; given 1 21
  • 22. 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”)Doesn’t matter unless you have an IBM 704 The names “car” and “cons” will come to have mnemonic significance only when we discuss the representation of the system in the computer. John McCarthy, Recursive Functions of Symbolic Expressions and Their Computation by Machine, Part I, 1960 22
  • 23. Implementing cons, car and cdr Next class: how to define cons, car, and cdr ourselves! 23
  • 24. ChargeQuiz Wednesday Course book through Chapter 4 Gleick’s The Information, Chapters 1-3 Classes 1-5 (including all questions in the class notes)Class Wednesday and Friday: Lots of examples and practice programming with procedures and recursive definitions 24