(define c (/ 13 3)) | ||
(define (foo x y) (* x y)) | ||
(a) | (+ c 3) | => |
(b) | (- 2 (foo c 9)) | => |
(c) | (foo -2 c) | => |
(d) | ((if (> (foo 2 3) 10) + -) c 3) | => |
(e) | ((lambda (x) (foo x c)) 12) |
=> |
(f) |
(let ((x (foo 2 4))) (+ x 5)) | => |
(g) |
(let ((a (foo c 9))) (let ((b (- a 1))) (+ b a))) | => |
(h) | (let ((c 3)) c) |
=> |
(i) | c | => |
(j) |
((let ((x 2) (y 3)) (lambda (x) (+ x y))) 8) | => |
f(n)= | 0 | if n=0 |
1 | if n=-1 | |
1 | if n=1 | |
2 * f(n+1) | if n <= -2 | |
n * f(n-2) | if n >= 2 |
STk> (f 2) 1.5 ; f(2)=f(1)+1/2=f(0)+1/1+1/2=3/2 STk> (f 3) 1.8333 ; f(3)=f(2)+1/3=3/2+1/3 STk> (f -3) 0 ; n<=1
STk> (sum-of-powers 3 2) 14 ; since 1**2 + 2**2 + 3**2 = 1 + 4 + 9 = 14
STk> (sum-harmonic 3) 1.83333333333333 ; since 1 + 1/2 + 1/3 = 1.83333333333333
STk> (my-quotient 7 3) should return 2 STk> (my-quotient 9 3) should return 3
STk> (find-digit? 3 15637) #T ; 3 appears in 15637 STk> (find-digit? 3 567) #F ; 3 does not appear in 567
STk> (add-even 12456) 12 ; 2+4+6=12 STk> (add-even 157) 0
(define (foo y) (let ((x (/ y 2))) (/ (+ y x) (- y x)))) (define (f x) (+ (foo x) x))What is the value of
(f 10)
?
(precedes el lst)
that
returns a list of all the elements in the list that immediately
precede el
in the list. The order of the elements
in the resulting list is not important.
It should work like this:
(el+pos lst)
that returns a list of each
element in lst added to its position in the list. Assume positions in
the list are numbered from 0, and that all the elements in the list are
numbers. It should work like this:
STk> (el+pos '(7 5 1 4)) (7 6 3 7) ; 7=7+0, 6=5+1, 3=1+2, 7=4+3
remove-adj-dup
, to remove all adjacent
duplicate elements in a list. If no adjacent elements are duplicate
it should return the original list. It should work like this:
(split lst)
to split a list into 2 lists and
to return a list of the two lists. The split is to be done so that the
first element from lst
will go into the first list, the second
into the second, the next into the first list, and so on. It should work
like this:
(compress lst)
that takes as argument
a list. The elements of the list are either symbols or lists containing
multiple copies of the same symbol. The procedure should return a list
where the repeated symbols are compressed, i.e. each sublist
of repeated symbols is replaced by a sublist of the form
( <number> <symbol>), where number is a positive integer which
indicates how many times the symbol occurred in the original sublist and
symbol is the repeated symbol.
set!, set-car!, set-cdr!
) in your procedure.
You are free to use as many auxiliary procedures as you like.
You do not need to do any error checking on the input list.
It should work like this:
STk> (compress '(a (very very) big boat)) (a (2 very) big boat) STk> (compress '((oh oh oh oh))) ((4 oh))
STk> (same-set? (list 5 4 2) (list 2 4 5)) #t STk> (same-set? (list 5 4 2 6) (list 2 4 5)) #f
STk> (adjoin-set 1 (list 2 3 7)) (1 2 3 7) STk> (adjoin-set 5 (list 2 3 7)) (2 3 5 7) STk> (adjoin-set 15 (list 2 3 7)) (2 3 7 15)
STk> (insert-el 3 (list 2 5 7)) (2 3 5 7) STk> (insert-el 5 (list 2 5 7)) (2 5 7)
STk> (remove-el 3 (list 2 3 7)) (2 7) STk> (remove-el 5 (list 2 3 7)) (2 3 7)
STk> (flatten '(a (b c (d e f) h)))
(a b c d e f h)
STk> (flatten ())
()
(multiplicity el tree)
to return the
number of occurrencs of an element in a tree.
It should work like this:
(greater-than n lst)
that counts how many
elements in a tree are strictly greater than n
.
It should work like this:
(samestructure tree1 tree2)
that checks whether two trees have the same structure.
Two trees have the same structure if they contain exactly
the same number of symbols and have all the parentheses
in the same positions. The symbols can be different.
It should work like this:
STk> (replace 'like 'dislike '(i (do not (like scheme)))) (i (do not (dislike scheme))) STk> (replace 'f 'g '(h (+ (f x y) (f 1 x)))) (h (+ (g x y) (g 1 x)))
STk> (find? 3 '(2 ((3 4) 7) 1)) #t STk> (find? 'a '(v ((c (d 7) 5)))) #f
STk> (extract number? '(2 ((3 4) 7) 1)) (2 3 4 7 1) STk> (extract number? '(v ((c (d 7) 5)))) (7 5)
STk> (deepen 'scheme '((and (scheme fun) (scheme weird)))) ((and ((scheme) fun) ((scheme) weird)))) STk> (deepen 'f '(h (+ (f x y) (f 1 x)))) (h (+ ((f) x y) ((f) 1 x))))
(define (foo y) (let ((x (/ y 2))) (/ (+ y x) (- y x)))) (define (f x) (+ (foo x) x)) (f 10)What is the value of the last expression
(f 10)
?
(define (make-scale r) (lambda (x) (* x r))) (define double make-scale) (define a (double 42))
(extend! lst item)
to extend a list by
attaching a new item at the end of the list. The procedure should
modify the existing list.
(replace! old new lst)
to destructively
replace the first occurrence of old
with new
in the list.
If the element does not appear in the list it should return the
list unchanged.
It should work like this:
STk> (replace! 'pet 'dog '(the cutest pet)) (the cutest dog) STk> (replace! 'pet 'dog '(the cutest cat)) (the cutest cat) STk> (replace! 1 100 '(3 4 1 2 1)) (3 4 100 2 100) STk> (replace! 1 100 (list 3 4 2 10)) (3 4 2 10)
(insert-one! item lst)
to destructively insert item
after the first element of
lst
. Assume the list always has at least one element.
It should work like this:
STk> (insert-one! 5 (list 1 2 3)) (1 5 2 3) STk> (insert-one! 5 (list 1)) (1 5)
delete-queue-rear!
that implements a new version of
delete-queue!
which operates at the other end of the queue, i.e
deletes off the rear of the queue instead of the front.
It should work like this:
STk> (define q (make-queue)) q STk> q (()) STk> (insert-queue! q 'a) ((a) a) STk> (insert-queue! q 'b) ((a b) b) STk> (delete-queue-rear! q) ((a) a)
(extract table key-list)
to find in a table the values of the keys
that are included in the list key-list
and return a list of them.
Assume that table
is a one-dimensional table, defined
as in the textbook.
offices
containing names of employees and their office numbers.
The keys in offices
are symbols representing names,
the values are office numbers.
Suppose you are also given a list names
with names of people.
(extract offices names)
should return a list containing
the office numbers of the names in names
who are in the table
offices
.
Use the selectors and mutators from the textbook
to access the entries in the table.
weekly-expenses
,
that is indexed by items you bought during the week (assume
they are symbols), and that contains for each item its price
(assume it is an integer).
Write a procedure add-table
to compute the total amount of money
you have spent to buy the items in weekly-expenses.
(increase! amount updates oldtable)
to increase by amount
the values of the keys in the
table oldtable
that appear in the list updates
.
The procedure increase!
should increase the value in
the table of all the entries whose
key is in the list updates
and ignore
any key in updates
which is not in oldtable
STk> (define accounts '(*table* (john . 250) (anna . 325) (bob . 50))) accounts STk> (define bonus '(john bob)) STk> (increase! 25 bonus accounts) (*table* (john . 275) (anna . 325) (bob . 75)))
(make-comparator initial)
to construct
an object that compares
if the item passed to it is the same as the item passed to it the previous
time.
The argument of make-comparator
is the initial value to
compare to.
The procedure should be written using message passing style.
The object created by make-comparator
should recognize the
following messages:
Message | Argument | Definition |
same? | item | if item is equal to the item passed the previous time it returns #t, otherwise it stores the item and returns #f |
last | returns the last item passed to it | |
reset! | resets the comparator |
STk> (define item (make-comparator ())) ;;creates an object with initial value () STk> ((item 'same?) 'a): #f ;; item a is saved in the object STk> (item 'last) a STk> ((item 'same?) 'a) #t ;; no change to the object STk> (item 'reset!) ()
(make-gauge init)
to create a
gauge object. A gauge object is an object that keeps a value and
counts up and down from it.
The object accepts the following messages:
Message | Definition | |
show | shows the value of the gauge | |
up! | increases the current value by 1 | |
down! | decreases the current value by 1 |
STk> (define the-g (make-gauge 0)) the-g STk> (the-g 'up!) 1 STk> (the-g 'show) 1
(make-counter init)
that returns a
procedure which,
when called, increases a counter by 1 and returns the counter value.
The initial value of the counter is init
.
The procedure make-counter
should
create an object with local state, so that every time it
is called it increases the counter by 1 and returns its value.
Assume that init
is a integer number.
It should work like this:
(make-box init)
to create a box
object.
A box object is an object that keeps a value and accepts the following
messages:
Message | Argument | Definition | |
show | shows the value in the box | ||
update! | replaces the current value with a new value | ||
reset! | resets the original value |
every
that returns true
if every element in a list satisfies a given predicate. This problem
was assigned in the 1st midterm. It should work like this:
>>> every(even,[1,3,7]) False >>> every(even,[2,4]) True
check_interval
to count
how many numbers in the range low to high are divisible by a number.
>>> check_interval(3,7,2) 2 # in the range 3 to 7 2 numbers, 4 and 6, are divisible by 2
>>> add_pairs([1,2,3,4]) [3,5,7]
>>> every_other2([1,2,3,4,5,6,7,8]) [2, 4, 6, 8] >>> every_other2([]) [] >>> every_other2([1]) []Copyright: © 2000-2007 by the Regents of the University of Minnesota