1 / 9

thirteen

thirteen. conditional expressions: letting programs make “decisions”. 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 … ]

abram
Download Presentation

thirteen

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 [procedureargs…] • 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. [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 A new kind of expression: if ►[define abs [n → [if [> n 0] n [- n]]]] <Procedure abs> ►[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 test1test2 … testn][or test1test2 … testn][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. Suppose we say: [define a 7] [define b 8] [define c 9.5] What are the values of [> a b] [> a b] [not [= b c]] [integer? c] [odd? a] [and [< 5 a] [< a 10]] Examples

  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. 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]] User-defined predicates

More Related