Download COMP3104Worksheet_Scheme

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
COMP3202 2013-14 Worksheet 5(a) The Scheme Language
Setting-up Dr.Scheme
Open up Dr.Scheme, Programs -> PLT Scheme -> DrScheme. Then from the menu bar select
Languages -> Choose Language. You’ll get a dialogue box. Select “How to Design Programs” then
“Intermediate Student”. You may need to change the language during the course of this worksheet.
Now, from the menu bar select Scheme -> Run.
You will see two windows. The top is the definitions window and the bottom is the interactions
window.
Tasks: Expressions
The following examples use the interactions window.
1. Type the number 69 in the interactions window and hit enter to see how scheme evaluates it. This
is a primitive expression. Now type in the string hello and read the error message.
2. Now type (+ 64 5) in the interactions window. NB there is a space between + and 64 and also a
space between 64 and 5. Scheme uses spaces to delineate atoms. Hit enter and check the addition.
Remember that (+ 64 5) is the prefix way of writing (64 + 5) as we would type in Java or C.
3. Now try (- 64 5) then (* 64 5) then (/ 64 5).
4. The prefix operator is useful since it can take an arbitrary number of arguments, for example try
(+ 64 5 1). Write this out as it would appear in Java or C.
5. We can also nest combinations, for example try (* 5 (+ 3 7)). Write this calculation out on paper,
then try it out. Now try this one ( * (+ 5 2) (+ 6 4)), first on paper.
6. Now over to you. Construct Scheme expressions for the following calculations:
(i)
3 x (6 + 4)
(ii)
(5 * x) + (6 + 4)
Tasks: Naming
7. In Scheme we name things with define. For example in the interactions window type
(define size 2)
size
Now type
(* 2 size)
And try
followed by
(* size size)
8. Let’s think about a circle. If a circle has a radius, then its circumference is 2 times pi times the
radius. Let’s calculate that. First we must define pi (let’s write “pie” since “pi” is defined internally)
(define pie 3.14159)
and the value for the radius which we’ll set to 10
(define radius 10)
Now complete the following expression to calculate the circumference (remember the prefix
operator can take as many arguments as you wish
(* ? ? ?)
Now the area of a circle is “pi radius squared”, ie pi times radius times radius. Write an expression
to calculate the area of the circle in the interactions window.
Tasks: Procedures
9. Procedures give us a way of chunking compound operations together so we can refer to them as a
unit. Let’s write a procedure to find the area of a circle. Here we go. Type this in the definitions
window
(define (area rad)
(* pi rad rad))
Now hit “Check Syntax” in the menu bar and then “Run”. Type “(area 2)” in the interactions
window and look at your result.
In the function define we have the declaration (area rad). This means that “area” is the name of
the procedure and “rad” is the argument of our function “area”. Then comes the “body” of the
function (* pi rad rad) tells us what our procedure “area” does. It takes the argument “rad” of
our procedure and multiplies it by itself and by pi.
Type this in the interactions window. That defines the procedure. To evaluate it type
(area 10)
And check the result. Try with a different parameter, eg 100.
10. Write a program to multiply the variable x by 7 in the definitions window. It will look
something like this
(define (seven x)
(
))
where something is missing. Test it in the interactions window by writing (seven 2) or something
else.
11. Write a procedure to calculate the circumference of a circle. Type this in the definitions window
following on from the area function. Hit “Check Syntax” then “Run” and test your program in the
interactions window
12. Write a program to calculate the average of two variables x and y in the definitions window. It
will look something like this.
( define (av x y)
( (
) ))
Test it in the interactions window by typing in something like (av 2 4).
13. Construct this procedure
(define (square x)
(* x x))
in the definitions window and test it in the interactions window, e.g., (square 10) .
We can use this procedure as a building block to make other procedures, for example
(define (sumsquares x y)
(+ (square x) (square y)))
Enter this into the definitions window and evaluate it by typing
(sumsquares 3 4)
in the interactions window. Check that the evaluation of this expression is correct! (Think
triangles!)
Tasks: Conditionals
14. Let’s say we want to test if the number x is greater than zero, zero or less than zero. We use the
following conditional expression
(define (test x)
(cond ( (> x 0)1)
( (= x 0)0)
( (< x 0)-1)))
Write this into the definitions (top) window and hit “check syntax” then “execute”. Now in the
interactions window type in the following to evaluate the procedure,
(test 2)
(test -4)
(test 0)
There is also an if-statement
(define (test2 x)
(if (< x 0)
(- x)
x))
Add this to your code in the defnitions window and execute. Find out what it does by entering the
following two examples in the interactions window
(test2 -3)
(test2 88)
Tasks: Recursion
15. Let’s run the example from class where we calculate the sum of numbers down from n to 1. For
example, for n=4 we calculate 4 + 3 + 2 + 1 = 10. We saw in class, that the sum form n down to one
is the same as the sum of n with the sum from n-1 down to one. This is expressed in the scheme
expression
(+ n (sum(- n 1))
We also saw in class that we could call this procedure recursively which means that the procedure
calls itself. So, to get the sum of “4 down to 1” we do “ 4 plus the sum of 3 down” which then
becomes “4 plus 3 plus the sum of 2 down” and so on. Here’s the procedure definition
(define (sum n)
(if (= n 1)1
(+ n (sum(- n 1)))))
Type this in the definitions window, followed by the expression
(sum 4)
Hit the “check syntax” button, then his “Run”. The interactions window will give you the
evaluation.
16. While the above problem was simply a toy, this problem is for real. It’s about calculating n!
which means the “n’th factorial. For example 4! = 4x3x2x1 = 24. We can get a recursive solution
since
4! = 4 x 3!
and so on. Here’s most of the code written for you. See if you can fill in the missing part
(define (fac n)
(if (= n 1)1
(
)))
And insert into the definitions window followed by (fac 4).
17. [hard] The “Fibonacci” form a sequence where each number is the sum of the preceding two,
like this
0,1,1,2,3,5,8,13,21,
They can be generated by the recursive rule
If n = 0 Fib(n) = 0
If n = 1 Fib(n) = 1
Otherwise
Fib(n) = Fib(n-1) + Fib(n-2)
Write a recursive procedure to calculate the n’th Fibonacci number. Clearly you need to use two
conditional expressions.
Stop here and move on to the second worksheet. The following is extension material for your
own self-study.
Tasks: Manipulating Lists
18. This question is about how to make and how to access pairs. This is a compound data structure
that can be built using the primitive procedure cons. Set the language to “Standard (R5RS)” Now
define a pair like this
(define x (cons 1 2))
Evaluate it by typing x in the interactions window. This will return the pair 1.2. To access the
values of the pair, we use
(car x)
and
(cdr x)
Try this out. Now try this construction
(define a (cons 1 (cons 2( cons 3 '()))))
Here ‘() refers to the “null” element. Any idea what this will give for a? Try it out. You will find
you have created a list of three numbers.
Now try
(car a) and (cdr a)
You should find that car gives the first element in the list and cdr gives the remaining elements.
Now what do you suppose this will do?
(cdr (cdr a))
And how about this
(car (cdr a))
And finally this
(car (cdr (cdr a)))
Note how this evaluates to something different from the expression (cdr(cdr a)). While car gives
you the first element in the list cdr gives you the remaining elements in the list, even though it is
one.
19. There is a primitive called list in scheme which helps producing a list. For example, produce the
list.
(define mylist (list 1 2 3 4))
Try various nestings of car and cdr on mylist. Can you work out how to get each individual member
of the list using car and cdr?
20. We can perform many interesting operations on lists, eg searching a list. Consider the following
Scheme code:
(define (listSearch items n)
(if (= n 0)
(car items)
(listSearch (cdr items) (- n 1))))
will run down the list and retrieve the n’th element. Let’s make a list, eg like this
(define shortlist (list 2 4 6 8 10 12))
then we can apply our procedure
(listSearch shortlist 3)
Check this out.