{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

05-functional-part2 - Functional Programming Part 2 Outline...

Info icon This preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Functional Programming Part 2 Outline Previous lecture Functional Programming Lecture LISP Some basics of Scheme Today’s lecture More on Scheme determine the meaning of scheme expressions by developing a precise model for interpreting them model: Substitution Model Linear recursive, Linear iterative, Tree recursive processes Scheme Expressions Primitive expression Example: Numbers Compound expression Combination of primitive expressions (+ 123 456) When you type an expression, the Scheme interpreter responds by displaying the result of its evaluating that expression
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
2 Scheme combinations Expressions formed by delimiting a list of expressions within parentheses in order to denote procedure application ( fun op1 op2 op3 …) delimited by parentheses first element is the Operator the rest are Operands Some simple combinations (+ 2 3) (- 4 1) (+ 2 3 4) (abs -7) (abs (+ (- x1 x2) (- y1 y2))) Meaning? What does a scheme expression mean? How do we know what value will be calculated by an expression? (abs (+ (- x1 x2) (- y1 y2)))
Image of page 2
3 Substitution Model To evaluate a scheme expression: 1. Evaluate each of the operands 2. Evaluate the operator 3. Apply the operator to the evaluated operands (fun op1 op2 op3 …) 1 2 3 Example of expression evaluation (+ 3 (* 4 5) ) operator operand 1 operand 2 Example of expression evaluation example: (+ 3 (* 4 5)) evaluate 3 3 evaluate (* 4 5) evaluate 4 4 evaluate 5 5 evaluate * * (multiplication) apply * to 4, 5 20 evaluate + + (addition) apply + to 3, 20 23
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
4 Primitive expressions Numbers evaluate to themselves 105 105 Primitive procedures evaluate to their corresponding internal procedures +, -, *, /, abs … e.g. “+” evaluates to the procedure that adds numbers together Another example (+ 3 (- 4 (* 5 (expt 2 2)))) evaluate 3 3 evaluate (- 4 (* 5 (expt 2 2))) evaluate 4 4 evaluate (* 5 (expt 2 2)) evaluate 5 5 evaluate (expt 2 2) evaluate 2 2 evaluate 2 2 apply expt to 2, 2 4 apply * to 5, 4 20 apply – to 4, 20 -16 apply + to 3, -16 -13 Note: Operator evaluation step was skipped Evaluation with variables Association between a variable and its value (define X 3) To evaluate a variable: look up the value associated with the variable e.g., X has the value of 3 and replace the variable with its value
Image of page 4
5 Simple expression evaluation (define X 3) (define Y 4) (+ X Y) evaluate X 3 evaluate Y 4 evaluate + [primitive procedure +] apply + to 3, 4 7 Special forms There are a few special forms which do not evaluate in quite the same way as we’ve described define is one of them
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}