; java -cp clojure.jar clojure.main
; (load-file "E:/Programming/Clojure programs on E/sharks.clj")
; (load-file "/Volumes/THUMBDRIVE/Programming/Clojure programs on E/sharks.clj")

(defstruct shark :id :direction :weight :hunger :status)

(defn make-shark
  "Defines a shark with :id = n and various attributes."
  [n]
  (struct-map shark
    :id n
    :direction (if (< (rand) 0.5)
      :left
      :right )
    :weight  (+ 90 (* 20 (rand)))
    :hunger 0
    :status :alive) )

(defn str-shark
  "Returns a string representation of a shark (or () for an empty list)."
  [shark]
  (cond
    (= shark ()) "()"
    (= (shark :direction) :left) (concat "<" (str (shark :id)))
    :else  (concat (str (shark :id)) ">") ) )

(defn print-sharks
  "Prints out a list containing sharks and empty locations."
  [lst]
  (println (map str-shark lst)) )
  
(defn make-shark-list 
  "Makes a list of how-many actual sharks, and some number of empty locations."
  ([how-many] (make-shark-list [] 1 how-many))
  ([lst n how-many]
    (cond
      (> n how-many) lst
      (> (rand) 0.75) (cons '() (make-shark-list lst n how-many))
      :else (cons (make-shark n) (make-shark-list lst (inc n) how-many)) ) ) )

(defn run-me []
  (do
    (print-sharks (make-shark-list 3))
    (print-sharks (make-shark-list 3))
    (print-sharks (make-shark-list 3))
    (print-sharks (make-shark-list 3)) ) )

(defn run-me-too []
  (do
    (let [
      ; create a shark called Sherman
      sherman (make-shark 1)
      ; create a reference to Sherman
      look!-a-shark! (ref sherman)]
      ; print Sherman in two different ways
      (println sherman)
      (println (str-shark sherman))
      ; put Sherman on a diet
      (dosync
        (ref-set look!-a-shark! (assoc @look!-a-shark! :hunger 4))
        (ref-set look!-a-shark!
          (assoc  @look!-a-shark! :weight (* 0.75 (@look!-a-shark! :weight))) )
        (ref-set look!-a-shark! (assoc @look!-a-shark! :status :ill-tempered))
      ; show off Sherman's new look
      (println @look!-a-shark!)
      (println (str-shark @look!-a-shark!))
      ; sorry, Sherman, you're immutable
      (println sherman)
      (println (str-shark sherman)) ) ) ) )

      
