Finish implementing the functions below. Use the get-block function defined in lecture as a helper function in your implementation. Also, if your functions make any assignments (setq), then the variables to which values are assigned must be local variables created by let or let*.
You can create test data for the functions in this section using the following assignment statements:
(setq blockA '((color red) (height 2) (width 3) (depth 3)))
(setq blockB '((color blue) (height 1) (width 2) (depth 3)))
(setq blockC '((color green) (radius 3)))
(setq world1 (list (list 'A blockA) (list 'B blockB) (list 'C blockC)))
Your code should work for any blocks and world in the same format as the above and for any number of blocks.
1. (defun get-volume (name world)
;; Returns the volume of the block
named <name> in <world>,
;; where volume
of non-spherical block is (height*depth*width), and
;; volume of
spherical block is (radius)3
;; Ex.
(get-volume 'A world1) returns 18
2. (defun is-spherical (name world)
;; Returns nil iff the block named
<name> in <world> is not spherical.
;; Ex. (is-spherical 'A) returns
nil
;; Ex. (is-spherical 'C) returns
t
3. (defun biggest-cuboid (world)
;; Returns non-spherical block
with largest horizontal surface (width * depth) in
;; <world>.
;; Ex. (biggest-cuboid world1)
returns
;;
((color red) (name A) (height 2) (width 3) (depth 3))
4. (defun can-put-on (name1 name2 world)
;; Returns t iff it is safe to put <name1>
on <name2> in <world>. It is safe iff
;; <name1> and <name2> are not
spherical and the top of <name2> has an area
;; equal to or greater than the bottom
of <name1> and nothing is already on <name2>;
;; or, if <name2> is the Table, then
any non-sphere can be put on it.
;; Ex. (can-put-on 'A 'Table world1)
returns t
;;
(can-put-on 'A 'C world1) returns nil
;;
(can-put-on 'B 'A world1) returns t
;;
(can-put-on 'A 'B world1) returns nil
5. (defun put-on (obj1 obj2 world)
;; Returns modified copy of <world>
in which <obj1> is on <obj2>. (The information
;; is stored in the association
list for <obj1> in the form: (on <obj1> <obj2>).
;; Warning: This function does
not check whether it is safe to put <obj1> on <obj2>
;;
and may create an unstable situation!
;; Ex. (put-on 'A 'B world1) returns
;; ( ((color red) (name A) (height 2)
(width 3) (depth 3) (on A B) ) ;; not safe!
;; ((color blue) (name B)
(height 1) (width 2) (depth 3))
;; ((color green) (name
C) (radius 3)) )
6. (defun build-tower (world)
;; Returns modified copy of <world>
in which the tallest possible safe tower has
;; been built by this
function. A tower is represented by a set of blocks O1
.. On
;; such that if O1
is the top of the tower, then for each Oi its description contains
;; the information
(on
Oi Oi+1). If no safe tower can be
built, then this returns nil.
;; Ex. (build-tower world1) returns
;; ( ((color red) (name A) (height 2) (width
3) (depth 3) (on A Table))
;; ((color blue) (name B) (height
1) (width 2) (depth 3) (on B A))
;; ((color green) (name C) (radius
3)) )
;; Implementation: you must use recursion
to implement this!
7. (defun number-blocks-in-tower (world)
;; Returns how many blocks in a tower
in <world>. You can assume there is no more
;; than one
tower. If there is no tower, then this returns 0.
;; Ex. (number-blocks-in-tower
(build-tower world1)) returns 2
;; Implementation: you must use recursion
to implement this!
8. (defun height-tower (world)
;; Returns height of a tower in <world>.
You can assume there is no more
;; than one
tower. The height is the sum of the heights of the blocks in
the tower. If there
;; is no tower,
then this returns 0.
;; Ex. (height-tower (build-tower
world1)) returns 3
;; Implementation: you must use recursion
to implement this!