# eight

Document Sample

```					                        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 …]
   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
A new kind of expression: if
[if test consequent                  ► [define abs
alternative]                         [n → [if [> n 0]
 If test is true,                                 n
   Then evaluate and return                  [- n]]]]
consequent                   <Procedure abs>
   Otherwise, evaluate and
return alternative
► [abs 5]
5
   Some useful tests                ► [abs -5]
   [= a b]                      5
Checks if a and b are the   ►
same
   [> a b], [≤ a b], etc.
Compares numbers
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 test1 test2 … testn]
[or test1 test2 … 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
   They’re named true and false.
Examples
Suppose we say:    What are the values of
[define a 7]      [> a b]
[define b 8]      [> a b]
[define c 9.5]
 [not [= b c]]

 [integer? c]

 [odd? a]

 [and [< 5 a]
[< a 10]]
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
   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
User-defined predicates
   Predicates are just         [define big-and-odd?
normal procedures              [n → [and [> n 10000]
   That happen to return                [odd? n]]]]
Booleans

[big-and-odd? 239803]
   So you can (and will)
[and [big-and-odd? b]
[< b 1000000]]
   (sorry, you’ll see a less
lame example soon)

```
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
 views: 0 posted: 7/1/2013 language: English pages: 9
How are you planning on using Docstoc?