CS 37

Clab 1: Intro to Scheme


Welcome to cs 37.
Clabs will usually be part lectures, part demos,(the class part), plus independent work on your part (the lab part), hence clab. We won't do much of the independent work today, but it will come.
The purpose of this clab is to welcome you to CS37 and introduce you to Scheme and the DrScheme environment. Feel free to experiment and do not hesitate to ask questions.

When you start drscheme by typing drscheme in a terminal window, a new window will open up. Before you can do anything, you will need to select a language from the language pull down menu. Usually we will use the standard R5RS which is at the top of professional languages. Select it, click ok, then selct run from the top of the drscheme window. The upper drscheme window is called the definitions window. The lower window is called the interaction window. We will try the following together:

(+ 17 3)

(/ 10 6)

You may not always get the same answers as the text. DrScheme uses exact arithmetic when possible. If you want to get results like the text, you can use the operator exact->inexact. For example, (exact->inexact (/ 10 6)) will give the result 1.6666666666666667 instead of 5/3 which is what DrScheme will yield using exact arithmetic in evaluating (/ 10 6).

(+ 137 349)
(- 1000 334)
(* 5 99)
(/ 10 5)
(+ 2.7 10)
(* 25 4 12)
(+ (* 3 5) (- 10 6))

(+ (* 3 5) (- 10 6) )
Change you language level to beginning student under teaching languages in the language pull down. Let's try (+ (* 3 5) (- 10 6)) in the definitions window and then click on step (the foot).

Now we'll start naming things:

In the definitions window:
(define swatrank 3); new mistaken USNews ranking in defs window
actually I think we are tie for number 1 but let's be modest.

(define havrank 10) in defs window

(define bmcrank 25) in defs window

Click on the Run button at the top of the window.
Put (/ havrank swatrank) in interaction window so you can see that we are still 3 times better :-)

(define sqrit (lambda (x) (* x x))) in defs window and click execute
(sqrit 7) in int window
A short hand for (define f (lambda (x) ...)) is
(define (f x) ...) so
(define (square x) (* x x)) is also a squaring function. Try it.
Try (= (sqrit 5) (square 5))

Now for some fun if time:

(define FtoC (lambda (x) (/ (* 5 (- x 32)) 9))) (define CtoF (lambda (x) (+ 32 (/ (* x 9) 5)))) (define describe (lambda (c) (cond ((<= (CtoF c) 32) "brrr!") ((and (> (CtoF c) 32) (<= (CtoF c) 50)) "chilly") ((and (> (CtoF c) 50) (<= (CtoF c) 70)) "not bad") ((and (> (CtoF c) 70) (<= (CtoF c) 85)) "warm") (else "hot!"))))

Recursion


; in definition window (define fact (lambda (n) (cond ( (= n 0) 1) (#t (* n (fact (- n 1)))) ) ) ) ;in interaction window try (fact 3) (fact 5) Put (fact 4) in the definition window and invoke step.
Keep steping watching the action. Make sure you understand what is happening. Call me over if you have questions.

If you are getting concerned about defining valuable procedures and having them disappear when you are finished, just select save definitions as from the File menu. Save often.


Have a great semester!