CS 119 Lab 1 – Scheme Fundamentals
Objectives
Perform the following tasks in the order given.
1. Retrieve the files words.scm, quilting.scm, and lab1.scm from the course web page.
2. Start up the application Edscheme5.0. Within Edscheme, open the two documents, words.scm and lab1.scm. Under the Evaluate menu select Document. You will need to do this for both documents. You should see the message, "Document evaluated successfully" each time in the Transcript window.
3. Within the Transcript window you will see the prompt Þ . You may enter scheme expressions at the prompt. An expression is always of the form (<operator> <operands>)
Try entering the following expressions and make sure you understand what they are doing:
(+ 2 3)
(* 5 6)
(word ‘comp ‘uter)
(sentence ‘(foundations of computer) ‘science)
(+ (/ 6 2) (- 10 4))
4. There is a special operator define which allow us to give names to constants.
Try the following in the Transcript window:
(define size 10)
(* 5 size)
The operator define may also be used to define our own functions or operators. The form of define for this purpose is
(define (<fnName> <arguments>) <fnBody>)
Try the following and make sure you understand what is happening:
(define (square x) (* x x))
(square 7)(define (add-s wd) (word wd ‘s))
(add-s ‘computer)(define (third-person verb) (sentence ‘she (add-s verb)))
(third-person ‘sing)
|
Assignment: |
5. The function (quote <arg>) returns the argument as given without trying to evaluate it. Examples are (quote apple) and (quote (apple juice)). Because quote is so common, we may abbreviate it with ‘. Examples are ‘apple and ‘(apple juice). We will use quote to enter words and sentences.
Try the following function on both words, like ‘apple, and sentences, like ‘(apple juice), and figure out what they do.
(first <word>) (first <sentence>)
(last <word>) (last <sentence>)
(butfirst <word>) (butfirst <sentence>)
(butlast <word>) (butlast <sentence>)
(item <integer> <word>) (item <integer> <sentence>)
Note: butfirst and butlast may be abbreviated as bf and bl.
|
Assignment: |
6. Section 1.3 in your text describes a quilting application where larger quilts are built out of primitive basic blocks. The primitive basic blocks are shown on p16. There are also two basic operations, stack and quarter-turn-right. These basic quilting functions are defined in the file quilting.scm. Evaluate this file and try out the examples in exercise 1.8 on p16 so that you understand how they work. When you perform a graphics operation in Edscheme, it will open up a new window containing your graphics. If you don’t see the new window (because it might be hidden by your other windows), check out the list under the Window menu. Your graphics window will simply be labeled by a number.
|
Assignment: |
7. A function which returns true of false (#t or #f) is called a predicate.
Try the following predicates and figure out what they do.
(= <number> <number>)
(> <number> <number>)
(before? <word> <word>)
(member? <letter> <word>) (member? <word> <sentence>)
(empty? <word>) (empty? <sentence>)
More complicated predicates may be formed by using the operators and, or¸ and not.
For example, (and (not (empty? wd)) (member? wd sent)).
Predicates may be used in two special functions if and cond.
The form of if is (if <pred> <expr-true> <expr-false). If <pred> evaluates to true then <expr-true> is evaluated. Otherwise <expr-false> is evaluated.
The form of cond is
(cond (<pred1> <expr1>)
(<pred2> <expr2>)
..
(<predn> <exprn>)
(else <exprn+1))
This is a little bit like a switch statement in Java. Scheme finds the first predicate in the list which is true and evaluates the corresponding expression. All other expressions are ignored.
Take a look at the functions is the file lab1.scm which demonstrate both the if and the cond functions. See if you can figure out what they are doing.
|
Assignment: |
8. Email your files containing the assignments to jzimmerm@goucher.edu.