90 likes | 208 Vues
This guide delves into conditional expressions in the Meta language, showcasing how programs can make decisions through Boolean evaluations. Explore the use of constants and variables, as well as how procedures are structured and executed with specific arguments. Discover how to define new names and create user-defined predicates that return Boolean values. Learn about useful tests for data types, comparisons, and the significance of truth values in logical expressions. This comprehensive overview will enhance your programming skills within the framework of conditional logic.
E N D
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 [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
[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 ►
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
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.
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
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
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
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