Ch1 HW3: Cow Graze

Assignment

Write the GRAZE function.

Extra credit: cows in a pasture without at least one grass die (are removed), leaving only the empty pasture.

Example

There are three pastures with grass in this example, two inside a pasture that only has sub-pastures.

|-------------|
|  (1)        |
| G COW COW   |
| G G G       |
|-------------|
||---|  |----||
||G G|  |G(3)||
||G G|  |COW ||
||G  |  |G G ||
||(2)|  |G G ||
||---|  |----||
|-------------|

Written in Lisp:

(define 
  pasture-ex
    '((G COW COW G G G)
      ((G G G G G)
       (G G G COW))))

Tests

Cow is C, grass is G.

(define-test test-graze
  "Make sure the grazing works."
  (assert-equal '(C C) (graze '(G C C G G)))
  (assert-equal '((G G G G) (C C))
                (graze '((G G G G) (G C G G C))))
  (assert-equal '((C C)
                  ((G G G G)
                   (C C)))
                (graze '((G C C G G)
                        ((G G G G)
                         (G C C G G))))))