Practicum 2
Due: Thursday, 7 February.
Objectives:
- Understand local environments.
- Be able to create functions (lambda).
Reference: The Scheme Programming Language,
R. Kent Dybvig, Addison-Wesley, 2003.
Requirements:
- Write solutions to the following problems in the definitions window
of DrScheme. Save them in a
file and name the file with your last name and a .scm extension.
Drag the file into
the user/faculty/barrj/CS 321/TurnIn/Practicum 2 folder.
- In the file that you turn in, place all of the test calls given
in the problem. This way, when your file is executed the solutions should
appear automatically in the interactions window.
- You must turn in a hard copy of your procedures.
- You must put your name and the practicum number in a comment at the top
of your solution.
- You may not use any imperative features like set!
in any of your solutions.
- You must work individually!
Practicum:
- Define a procedure called third that takes one parameter that is a
list and that returns the third item in the list. The list may or may not
contain three items. You must check to make sure that the argument is a
list (and not some other data type). You may not use the list operator in
your solution. Test your procedure on:
- '(a b c d) should return c
- '(a) should return '()
- '((a) (b c) (d e) (f g)) should return (d e)
- '() should return '()
- Define a procedure juggle that rotates a three-element
list. The procedure juggle returns a list that is a rearrangement of
the input list so that the first element of this list becomes the second,
the second element becomes the third, and the third element becomes the
first. You may not use the list operator in
your solution. You may assume that the argument you receive is a list of
three items. Test your procedure on:
- '(jump quick spot)
- '(dog bites man)
- (1 2 3)
- Define a function subst-item(new old ls) that takes three parameters:
an item
new, an item old, and a list of items ls. The list may
or may not be empty. The function subst-item looks for all
top-level occurrences of the item old in ls and replaces it
with the item new. Test your function on the following items.
You may not use the list operator in
your solution.
Hint: use equal? to test items.
- (subst-item 'dog 'cat (my cat is clever))
- (subst-item 'b 'a '(c a b a c a c))
- (subst-item '(0) '(*) '((*) (1) (*) (2) (*) (*)))
- (subst-item 'two 'one '()))
|
|
|
 |