Presentation is loading. Please wait.

Presentation is loading. Please wait.

Thirteen conditional expressions: letting programs make “decisions”

Similar presentations


Presentation on theme: "Thirteen conditional expressions: letting programs make “decisions”"— Presentation transcript:

1 thirteen conditional expressions: letting programs make “decisions”

2 Recap: The Meta language Names: constants and variables When evaluated, return a specific data objects Can make new names with: [define name value] Procedure calls [procedure args …] Procedure is run with the args as inputs Compound procedures [args … → exp] Makes a new procedure with the specified arguments and return value Expression for return value can refer to args Define and with [define name exp] [with name = exp … exp] Introduce new names or values for names

3 A new kind of expression: if [if test consequent alternative] If test is true, Then evaluate and return consequent Otherwise, evaluate and return alternative Some useful tests [= a b] Checks if a and b are the same [> a b], [≤ a b], etc. Compares numbers ► [define abs [n → [if [> n 0] n [- n]]]] ► [abs 5] 5 ► [abs -5] 5 ►

4 Some other useful tests [number? value], [string? value], [bitmap? value], [integer? value], [procedure? value] Tests what kind of data value is [odd? number], [even? number] Tests whether a number is odd or even Okay, maybe this isn’t so useful … [and test 1 test 2 … test n ] [or test 1 test 2 … test n ] [not test] Combines tests into more complicated tests

5 Boolean objects Everything in Meta is an expression, And all expressions have values, So then what kind of value is [= a b] ? Answer: a “truth value” – true or false These are named Booleans after George Boole, who invented Boolean Algebra, an early form of symbolic logic Meta has two magic data objects that are used to represent the answers to questions They’re named true and false.

6 Examples Suppose we say: [define a 7] [define b 8] [define c 9.5] What are the values of [> a b] [not [= b c]] [integer? c] [odd? a] [and [< 5 a] [< a 10]]

7 Executing a Boolean expression Evaluating tests is really just normal procedure execution [and [< 5 a] [< a 10]] First, execute [< 5 a] Call < with 5 and 7 as inputs < returns true Then call [< a 10] Call < with 7 and 10 as inputs < returns true Call and with true and true as arguments (actually, this part is a little more complicated, but that won’t matter until next quarter) And returns true

8 Predicates (question answerers) Procedures, like = or odd?, that return Booleans are called predicates They can be thought of as tests or question answerers [= 1 2] asks the question “are 1 and 2 the same?” [odd? 7] asks the question “is 7 an odd number?” And their return values can be thought of as the answers [= 1 2] returns false [odd? 7] returns true Predicates are an important type of procedure

9 User-defined predicates Predicates are just normal procedures That happen to return Booleans So you can (and will) write your own (sorry, you’ll see a less lame example soon) [define big-and-odd? [n → [and [> n 10000] [odd? n]]]] [big-and-odd? 239803] [and [big-and-odd? b] [< b 1000000]]


Download ppt "Thirteen conditional expressions: letting programs make “decisions”"

Similar presentations


Ads by Google