CS1: Introduction to Computation - PowerPoint PPT Presentation

1 / 42
About This Presentation
Title:

CS1: Introduction to Computation

Description:

Usually back of 'Big Idea' handout (More admin at end of class) CALTECH CS1 Fall2002 -- DeHon ... Evaluate Y 4. Apply to 3,4 7. CALTECH CS1 Fall2002 -- DeHon ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 43
Provided by: andre578
Category:

less

Transcript and Presenter's Notes

Title: CS1: Introduction to Computation


1
CS1Introduction to Computation
  • Day 2 October 2, 2002
  • Substitution Model

Add cards (and questions) AFTER class.
2
Precision
  • Human (natural) Language is imprecise
  • Full of ambiguity
  • Computer languages must be precise
  • Only one meaning
  • No ambiguity

3
Last Time
  • Saw how to form Scheme expressions

4
Today
  • Define their meaning
  • By developing a precise model for interpreting
    them
  • Model Substitution Model

5
Admin 1
  • We do post slides after class
  • see course schedule online
  • Will try to post preliminary slides before class
  • Same place
  • Probably by 11am (prob. with a few errors)
  • No promise
  • Put Scheme code on handout
  • Usually back of Big Idea handout
  • (More admin at end of class)

6
Scheme Combinations
  • Scheme combination
  • (fun op1 op2 op3 )
  • Delimited by parentheses
  • First element is the Operator
  • Rest are Operands

7
Scheme Combinations
  • ( 2 3)
  • (- 4 1)
  • ( 2 3 4)
  • (abs -7)
  • (abs ( (- x1 x2) (- y1 y2)))

8
Meaning?
  • What does a Scheme expression mean?
  • How do we know what value will be calculated by
    an expression?
  • (abs ( (- x1 x2) (- y1 y2)))

9
Substitution Model
  • To evaluate a Scheme expression
  • Evaluate its operands
  • Evaluate the operator
  • Apply the operator to the evaluated operands
  • (fun op1 op2 op3 )

10
Example Expression Eval
  • Example ( 3 ( 4 5) )

11
Example Expression Eval
  • Example ( 3 ( 4 5))
  • Evaluate 3
  • Evaluate ( 4 5)
  • Evaluate 4
  • Evaluate 5
  • Evaluate
  • Apply to 4, 5 ? 20
  • Evaluate
  • Apply to 3, 20 ? 23

12
Another Example
  • ( 3 (- 4 ( 5 (expt 2 2))))
  • Evaluate 3
  • Evaluate (- 4 ( 5 (expt 2 2)))
  • Evaluate 4
  • Evaluate ( 5 (expt 2 2))
  • Evaluate 5
  • Evaluate (expt 2 2)
  • Evaluate 2
  • Evaluate 2
  • Apply expt to 2,2 ? 4
  • Apply to 5,4 ? 20
  • Apply to 4, 20 ? -16
  • Apply to 3, -16 ? -13

13
Evaluation with Variables
  • An assignment provides an association between a
    variable and its value
  • (define X 3)
  • To evaluate a variable
  • Lookup the value associated with the variable
  • And replace the variable with the value

14
Variable Evaluation Example
  • (define X 3)
  • Evaluate X
  • Lookup value of X
  • X has value 3
  • Result 3

15
Simple Expression Evaluation
  • Scheme Assignment and Evaluation
  • (define X 3)
  • (define Y 4)
  • Evaluate ( X Y)
  • Evaluate X ? 3
  • Evaluate Y ? 4
  • Apply to 3,4 ? 7

16
Special Forms
  • N.B. There are a few special forms which do not
    evaluate in quite the same way as weve
    described.
  • define is one of them
  • (define X 3)
  • We do not evaluate X before applying define to X
    and 3
  • We simply
  • Evaluate the second argument (3)
  • Make an association for it to the first argument
    (X)

17
Lambda
  • Lambda is also a special form
  • Result of a lambda is always a function
  • (an operator)
  • We do not evaluate its contents
  • Dont even treat them the same

18
Scheme Functional Definition
  • A(r) pi r2
  • (define A (lambda (r) ( pi (expt r 2))))

19
Evaluate a function call
  • To Evaluate a function call
  • Evaluate the arguments
  • Apply the function
  • To Apply a function call
  • Replace the function argument variable with the
    value given in the call everywhere it occurs
  • Evaluate the resulting expression

20
Informally
  • (define f
  • (lambda (x)
  • ( x 1)))
  • What does this function do?
  • (f 2)
  • Substitute 2 for x
  • (f 2)
  • ( 2 1)
  • 3

21
Evaluate a function call
  • (define f
  • (lambda (x)
  • ( x 1)))
  • Evaluate (f 2)
  • Evaluate 2
  • Evaluate f ? (lambda (x) ( x 1))
  • Apply (lambda (x) ( x 1)) to 2
  • Substitute 2 for x in the expression ( x 1) ?
    ( 2 1)
  • Evaluate ( 2 1) ? 3

22
Evaluate Function Call
  • (define f (lambda (x y)
  • ( ( 3 x) ( -4 y) 2))))
  • Evaluate (f 3 2)
  • Evaluate 3
  • Evaluate 2
  • Evaluate f ? (lambda (x y) ( ( 3 x) ( -4 y)
    2))
  • Apply f to 3 2
  • Substitute 3 for x, 2 for y
  • ( ( 3 3) ( -4 2) 2))
  • Evaluate . 3

23
Syntactic Sugar
  • Equivalent expressions
  • (define f (lambda (x) ( x 1))
  • (define (f x) ( x 1))
  • Simply an alternate syntax
  • Allows us not to write lambda
  • Maybe more natural for some
  • Means the same thing

24
Evaluating define
  • To evaluate (define (f x) ( x 1))
  • Desugar it into lambda form
  • (define f (lambda (x) ( x 1))
  • Evaluate like any define
  • Create an association between the name f and the
    function (lambda (x) ( x 1))

25
Example
  • (define sq (lambda (x) ( x x))
  • (define d (lambda (x y) ( (sq x) (sq y))))
  • Evaluate (d 3 4)
  • Evaluate 3?3
  • Evaluate 4?4
  • Evaluate d?(lambda (x y) ( (sq x) (sq y)))
  • Apply (lambda (x y) ) to 3 4

26
Example
  • Apply (lambda (x y) ( (sq x) (sq y))) to 3 4
  • Substitute 3 for x, 4 for y in ( (sq x) (sq y))
  • Evaluate ( (sq 3) (sq 4))
  • Evaluate (sq 3)
  • Evaluate 3?3
  • Evaluate sq?(lambda (x) ( x x))
  • Apply (lambda (x) ( x x)) to 3
  • Substitute 3 for x in ( x x)
  • Evaluate ( 3 3)
  • Evaluate 3?3
  • Evaluate 3?3
  • Apply to 3 3 ? 9

27
Example
  • Apply (lambda (x y) ( (sq x) (sq y))) to 3 4
  • Substitute 3 for x, 4 for y in ( (sq x) (sq y))
  • Evaluate ( (sq 3) (sq 4))
  • Evaluate (sq 3)?many steps, previous slide?9
  • Evalute (sq 4)
  • Evaluate 4?4
  • Evaluate sq?(lambda (x) ( x x))
  • Apply (lambda (x) ( x x)) to 4
  • Substitute 4 for x in ( x x)
  • Evaluate ( 4 4)
  • Evaluate 4?4
  • Evaluate 4?4
  • Apply to 4 4 ? 16

28
Example
  • Apply (lambda (x y) ( (sq x) (sq y))) to 3 4
  • Substitute 3 for x, 4 for y in ( (sq x) (sq y))
  • Evaluate ( (sq 3) (sq 4))
  • Evaluate (sq 3)?many steps, 2 slides back?9
  • Evaluate (sq 4)?many steps, previous slide?16
  • Apply to 9 and 16 ?25
  • which is final result
  • (d 3 4) ? 25

29
Substitution Model
  • Given precise model for evaluation
  • Can carry out mechanically
  • By you
  • By the computer
  • Will be basis of our understanding for first half
    of course (until midterm)
  • will expand and revise later

30
Use the Model
  • The model is simple
  • Yes, it is tedious to evaluate all those steps
  • But, thats how the machine works
  • You can understand anything by applying the model
    carefully
  • Dont skip steps

31
Uncertainty / Unknown
  • To be a good Scientist / Student
  • Must be comfortable with the unknown
  • comfortable with what you dont know
  • Dont be too quick to latch on to an answer
  • You may grab the wrong one
  • Be open to a better (the right) answer

32
Quote
  • To stay young requires unceasing cultivation of
    the ability to unlearn old falsehoods.
  • Notebooks of Lazarus Long

33
Big Ideas
  • Computer languages must be precise
  • Define their meaning with a model
  • Current model substitution model
  • Can use it to understand the meaning of any
    Scheme expression

34
Admin Wrapup
35
Unix Tutorial Section
  • Tonight!
  • At 10pm
  • In Jorgensen 74
  • By Jason Mitchell
  • ls, cd, mkdir, cat, more, rm, xterm, exit,
    scheme, .
  • (if any of these are foreign to yougo!)

36
Recitations
  • Start Tomorrow
  • Bring your questions!
  • Two recitations at every time slot
  • One is advanced
  • Other is beginner
  • Choose which one based on your comfort level

37
Advanced vs. Beginner
  • Beginner
  • Dont feel inhibited asking any question because
    its too basic
  • What is an operator?
  • Advanced
  • Dont feel inhibited (geeky) for asking a
    question because its too advanced
  • Why havent you told us about types?

38
Sections
  • Beginner
  • Thurs. 4pm Broad 100
  • Fri. 1pm Beckman 115
  • Fri. 2pm Beckman 115
  • Advanced
  • Thurs. 4pm Jorgensen 74
  • Fri. 1pm Broad 100
  • Fri. 2pm Broad 100

39
UGCS Accounts
  • Signup for your UGCS accounts
  • card access being handled as part of that
  • (not the way I told you on Monday)
  • Necessary to create your cs1man account
  • (probably wont be ready for submits until Monday)

40
Jorgensen Access
  • Only need cards after 530pm
  • If card not work
  • Phones by first floor doors
  • Call UGCS Lab 2028
  • (Call Security 4702)

41
Lab Sections
  • Now posted on the web
  • Linked from CS1 home page on UGCS
  • Not fillout form Monday?
  • Mail ben_at_ugcs.caltech.edu with 4 preferences
  • Start Sunday
  • Some of you will have lab sections before next
    lecture
  • Lab is staffed the rest of this week
  • through Friday midnight
  • Except 68pm

42
ACM Programming Contest
  • Team Practice
  • Tues 830-1030pm
  • Oct. 8th first meeting
  • Jorgensen 154
  • Mail ben_at_ugcs
Write a Comment
User Comments (0)
About PowerShow.com