Professional Documents
Culture Documents
There are also additional constants: #t for true and #f for false.
Other comparisons:
(< x y)
(> x y)
(<= x y)
(>= x y)
Not all arguments may be evaluated. (This is why and and or are
called special forms rather than functions.)
x when x < 0
|x| =
x when x 0
(cond
[(< x 0) ( x)]
[(>= x 0) x])
(cond
[question1 answer1]
[question2 answer2]
...
[questionk answerk])
(define n 5)
(cond [(even? n) "even"] [(odd? n) "odd"])
(cond [(even? 5) "even"] [(odd? n) "odd"])
(cond [false "even"] [(odd? n) "odd"])
(cond [(odd? n) "odd"])
(cond [(odd? 5) "odd"])
(cond [true "odd"])
"odd"
(define (add1-if-even n)
(+ n
(cond
[(even? n) 1]
[else 0])))
(or (= x 0)
(cond
[(positive? x) (> x 100)]
[else (< x 100)]))
Definition:
Nicer ordering:
(= (bonus 50) 0)
Questions to ask:
Does the string start with "cat"?
Does the string end with "cat"?
(define (cat-end? s)
(string=? "cat" (substring s ( (string-length s) 3) )))
(define (too-short? s)
(> 3 (string-length s)))
Symbols have a special marker to indicate that they are not function
names or strings.
Limitations:
No other comparisons or computations possible.
How many examples and tests are needed to cover all cases?
The predicate equal? can be used to test the equality of two values
which may or may not be of the same type.
a value,
a single constant,
a function application,
a conditional expression, or
a Boolean expression.
Recall: A value is a number, a symbol, a string, or a Boolean value.
A constant is a named value.
You should be able to trace programs using the substitution rules for
and, or, and cond.