CS 22

Clab 1: Intro to Scheme


Welcome to cs 20.
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 CS22 and introduce you to Scheme and the DrScheme environment. Feel free to experiment and do not hesitate to ask questions.

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) )
Let's try (+ (* 3 5) (- 10 6)) in the definitions window and then click on step (the foot).

Now we'll start naming things:

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

(define havrank 6) in defs window

(define bmcrank 16) in defs window

Click on the Execute 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!"))))

Remember to bring your text to next class.