Scheme returns 5. But now if you type5
Scheme returns:foo
Unbound variable: foo
Now if you type
Scheme returns:(+ 1 2)
3
Scheme returns 5. If you type5
Scheme returns "hello""hello"
Scheme returns #t#t
Scheme returns:foo
Error: the variable FOO is unbound
open-paren operator arguments close-paren ( + 1 2 )
(+ 1 2)
(+ (* 2 3) (- 5 2))
(list 1 2 3) (eq? a b) (- 4 3)
()
'(a b)
(quote (a b))
(> 3 2)
returns
#t
(> 2 3)
returns
() which is the way #f is printed
Predicate | Description |
---|---|
< | less than This is the opposite of > |
= | equals This operator returns t if its two arguments are numerically equal, #f otherwise. |
<= | less than or equal |
>= | greater than or equal |
define
:
(add-one (add-one 10)) 12
(define (positive? x) (>= x 0))
(define (twice? x y) (= x (* 2 y)))
When we call twice
with these arguments:
(twice 4 2)
The body is
(= x (* 2 y))
Where x is bound to 4 and y is bound to 2. So (* 2 y) returns 4, and x is 4, and = returns t, so twice returns t.
(define (zero? x) (= x 0)) (define (plus? x) (> x 0)) (define (minus? x) (< x 0))
(if (> 3 2) 1 2)
(define (zun arg) (if (= arg 0) 0 1))
So what happends if we call
(zun 3)
If we call(define (max x y) (if (> x y) x y))
(max 7 7)
Then the condition is #f (because 7 is not greater than 7), so the second consequent is evaluated. This returns 7. numbers.
(define (distance a b) (if (> a b) (- a b) (- b a)))
As in the case of max, if a and b are equal, then we end up subtracting b from a, but we would have gotten 0 in either case.
(not #t) ()(not #f) #t
(not (= 3 4)) #t
(and #t #t #t) #t
(and #t #f #t) ()
(or #f #f #f) ()
(or #t #t) #t
(define (either-zero x y) (or (= x 0) (= y 0))) (define (in-ten x) (if (and (> x 0) (< x 10)) x 5))
x! = x (x-1) (x-2) ...
So,
3! = 3*2*1 = 6 5! = 5*4*3*2*1 = 120
(define (fact x) (if (= x 0) 1 (* x (fact (- x 1))))
power
. The
power
function raises some number to a power by
multiplying the number times itself some number of times.
(define (power x n) (if (= n 0) 1 (* x (power x (- n 1)))))
power
function for the same value of x, and one
less than the value of n. The result is then multiplied by
x to yield the power
function for this value of
x and n.
cond
is the most general conditional in
Scheme. Where if
and when
and
unless
allow only a single test, cond
allows
any number. Here is an example use of cond
:
(define (frab x y) (cond ((< x y) (+ x y)) ((= x y) 0) (#t (* x y))))
cond
expression begins with the operator
cond
. This is followed by a sequence of
statements each enclosed in parentheses.
cond
statement begins with a condition and is
followed by any number of consequent forms.
cond
is:
((< x y) (+ x y))
(< x y)
and the single
consequent form is (+ x y)
.
cond
is interpreted is as follows: The
interpreter works on the statements in order. As it gets to each
statement, the condition part is evaluated. If it returns
#t
, the consequent forms are evaluated, and the
value of the last consequent form is the value of the
cond
. Once the condition part of a statement returns
#t
, and the associated consequents are evaluated,
no more statement forms are considered. If no condition part of a
statement in a cond
returns #t
, the
value of the cond
is #f
.
Could be written like this:(if < test> < true-consequent> < false-consequent>)
For example:(cond ((< test> < true-consequent>) (t < false-consequent>)))
Could be written:(if (> x y) (- x y) (- y x))
(cond ((> x y) (- x y)) (#t (- y x)))
If you type
(list 1 2 3)
Scheme returns
(1 2 3)
(list (+ 1 2) (- 5 3) (* 2 5) 7) (3 2 10 7)
(car (list 1 2 3)) 1
(cdr (list 1 2 3)) (2 3)
(car (cdr (list 1 2 3))) 2 (cdr (cdr (list 1 2 3))) (3) (cdr (cdr (cdr (list 1 2 3)))) ()
define
(we will see more about this later).
(define list-1 (list 1 2 3)) (1 2 3) (list? list-1) #t(list? (cdr list-1)) #t
And #f is considered a list also:
(list? #f) #t
(define (is-list? x) (and (list? x) (not (null? x))))
(define list-1 (list 1 2 3))(define list-2 (cons 0 list-1)) (0 1 2 3)
list-1 (1 2 3)
But the new list has the additional element:
list-2 (0 1 2 3)
(cons 1 #f) (1)
This is the same result as if you typed:
(list 1) (1)
(list? (list 1)) => #t (list? 1) => ()
(cons 1 (cons 2 (cons 3 #f))) (1 2 3)
----------------- | | | | first | rest | | (car) | (cdr) | -----------------
(define c-1 (cons 'a #f)) => (a)
would be drawn like this:
----------------- | | | | a | () | | | | -----------------
And this makes sense because if you typed
(car c-1) => a (cdr c-1) => ()
(define c2 (cons 'w (cons 'x ()))) => (w x)
looks like this:
---------------- | | | | w | * | | | | | -----------|---| | V ---------------- | | | | x | () | | | | ----------------
So here we have two cons cells. The car cons cell has a car which is the symbol w, the cdr of that points to another cons cell whose car is the symbol x and whose second is ().
---------------- | | | | w | * | | | | | -----------|---| | V ---------------- | | | | x | * | | | | | -----------|---| | V ---------------- | | | | y | () | | | | ----------------
This structure corresponds to what you would get by evaluating:
(cons 'w (cons 'x (cons 'y ())))
Note that this is the same as what you would get by evaluating
(list 'w 'x 'y)
or
'(w x y)
(define c3 '((a b) c))
That this correspond to what would be constructed by:
(cons (cons 'a (cons 'b ())) (cons 'c ()))
And this helps us draw the diagram:
---------------- ---------------- | | | | | | | * | *----------> | c | () | | | | | | | | ----|----------| ---------------- | V ---------------- | | | | a | * | | | | | -----------|---| | V ---------------- | | | | b | () | | | | ----------------
(define list-1 '(a b c)) (define list-2 '(a b c))(eq? list-1 list-2) ()
Even though both of them look the same: (a b c).
But a list is eq? to itself:
(eq? list-1 list-1) #t(eq? list-2 list-2) #t
(define (equal-1 a b) (cond ((eq? a b) #t) ((not (list? a)) #f) ((not (list? b)) #f) ((eq? (car a) (car b)) (equal-1 (cdr a) (cdr b)))))
(equal? '((a b) (c) d) '((a b) (c) d))
And have it return #t.
(define (equal? a b) (cond ((eq? a b) #t) ((not (list? a)) #f) ((not (list? b)) #f) ((equal? (car a) (car b)) (equal? (cdr a) (cdr b)))))
x l (note that this is different from the number 1) list a-real-long-variable var5
2.3 -3 -3.2 3e4
or(define LuckNumber 7)
(define LuckNumber (read)) assume 7 is entered on the keyboard
(set! LuckNumber 11) changes the value of LuckyNumber to 11
(let ((a 2)) (+ a 1))
(define a 1) (+ a 1) (let ((a 2)) (+ a 1)) (+ a 1)
(let ((a (+ 3 4))) (- a 2))
The value that a is bound to is 7. And so the value of (- a 2) is 5, which is the value of the whole expression.
(let ((a 1) (b 2)) (+ a b))
Binds a to 1 and b to 2 and then adds them up. So this yields 3.
(define a 1) (let ((b 2)) (+ a 1))
In this case a has a top-level binding to 1 and the let form binds b but not a. So the value that is used for a is the global value namely 1, so the value of the expression is 2.
(let ((b 2)) (+ a b))
In this case the value of b is obtained from the local environment, and the value of a is obtained from the top-level environment, so the result is 3.
(let ((a 1)) (let ((b 2)) (+ a b)))
In this case the outer let form binds a to 1. A let form can contain any Scheme expression as its body. So there is nothing wrong with putting another let form in the body. This let form binds b to 2. The body of the inner form is (+ a b).