drscheme
Click Language/Choose Language in the menu at the top of the window. Set your language option to Full Scheme.
You will see a pair of windows. The bottom window says "Welcome to DrScheme." That is the interactive window. This is where you can test statements to see what they will do. Try it out - in the interactive window, type
(+ 3 4)
This should add 3 to 4. In Scheme, + is a function. To make a function call in Scheme, you place the name of the function and its arguments, separated by spaces, inside parentheses. This takes a little getting used to! In C++ and Java, function calls look something like this:
function(arg1, arg2, arg3);
In Scheme, function calls look like this:
(function arg1 arg2 arg3)
(car '(apple orange pineapple))
(cdr '(apple orange pineapple))
(car (apple orange pineapple))
What do CAR and CDR do? Why is the single quote necessary? What does it do?
2. In the interactive window, write sequences of CARs and CDRs that will pick the symbol PEAR out of the following expressions:
(apple orange pear grapefruit)
(((apple) (orange) (pear) (grapefruit)))
(apple (orange) ((pear)) (((grapefruit))))
3. Execute the following statements. What do the functions cons, append, and list do?
;; This is a comment, by the way!
(cons 'x '(1 2))
(cons '(1 5) '(2 3))
(append '(1) '(2 3))
(append '(1 5) '(2 3))
(list '1 '2 '3 '(4 5))
4. Execute the following code. What do length and reverse do?
(length '(plato socrates aristotle))
(reverse '(plato socrates aristotle))
(member 'socrates '(plato socrates aristotle))
(member 'raphael '(plato socrates aristotle))
1. In the interactive window, type
(define x '(hi there))
(define y '(hi there))
(equal? x y)
(eq? x y)
What kind of responses do you get? Why?
2. Type
(define x 'hi)
(define y 'hi)
(equal? x y)
(eq? x y)
What kind of responses do you get?
3. Enter the following code:
;; The function = can be used on numbers
(define x 5)
(define y 0)
(if (= x 3)
(set! y 9)
(set! y 10))
What value is assigned to y? What's happening here? (Hint: if-then-else)
4. The following code acheives the same purpose, and is much cleaner. Why does it work?
(define x 5)
(define y (if (= x 3) 9 10))
5. Enter the following code:
(define x 8)
(define y (cond ((= x 3) (+ 3 8))
((= x 8) 12)
(else (* 6 3))))
Can you change the value of x to get different values of y? What does the cond function do?
(lambda (x)
(+ x 1))
What does Scheme return? The lambda function returns a procedure (function) without a name. In this case, you have defined a procedure to take one argument and add 1 to it.
2. A procedure without a name is useless, as it is immediately garbage collected. Try this instead:
(define add-one
(lambda (x)
(+ x 1)))
Then in the interactive window, type (add-one 5). The define statement created a pointer, called add-one, which points to the function you just created.
3. The above can be entered via shorthand as
(define (add-one x)
(+ x 1))
Try it! Remember, this is just shorthand for the slightly longer code above.
4. Try this for size:
(define another-add-one add-one)
(another-add-one 5)
At the pointer level, what is happening here? Draw a picture on this paper indicating what is happening.
5. You can declare "local" variables in Scheme via the use of the let function. For example, run the following code:
(define a 5)
(define b 6)
(define c 7)
(let ((a 1) (b 2))
(set! a 7)
(set! c (+ a b))
)
After executing this code, what are the values of a, b, and c? Why?
(define (mystery L)
(if (null? L)
L
(append (mystery (cdr L))
(list (car L)))))
Note that there is no "return" statement here, as you would find in a similar function in C++. Why? How is the return value determined in the above function?
2. To trace your program, enter
(require-library "trace.ss")
to the top of the program (in the definitions window), then execute.
Now type (in the interactive window)
(trace mystery)
(mystery '((1 2) (3 4) 5 6))
(untrace mystery)
By the way, to load a file containing function definitions that we want
to use without seeing (like a #include in C) use (load "file name").
3. Write a recursive function, keep-first-n, that returns a list of the first n elements in a list. You may assume that there are at least n elements.
Example:
> (keep-first-n 3 '(a b c d e f g h i))
(a b c)
(let loop ((count 0))
(display "Count = ")
(display count)
(newline)
(if (< count 10) (loop (+ count 1))))
"loop" is being called as if it were a function. What does this mean? Is this iteration or recursion?
2. Enter the following code:
(define x 8)
(define y 0)
(define z 0)
(if (= x 8)
(begin (set! y 2)
(set! z 3))
(begin (set! y 7)
(set! z 9))))
What purpose does "begin" serve?