[Cs254f11] Another q

James Matheson jmatheson12 at gmail.com
Sun Oct 9 21:24:47 EDT 2011


So thanks for the reply Omri it was very helpful.

Another question: here I'm creating a number of arithmetic equations,
eval-ing them, and finding the error. I'm using atoms to store data
from a vector. When I go to run, however, I get
#<CompilerException java.lang.Exception: Unsupported binding form:
(get-in data [i 0]) (NO_SOURCE_FILE:2137)>
Anyone know why this might be going on? Also, if I have any obvious
bugs in the code, please let me know!

N.B. The function I'm working on is at the end, the other parts of the
code are used within it.

;solution for div by zero
(defn div-none
  "This returns 0 if division is called with a second argument of 0."
  [x y]
  (if (= y 0)
    0
    (/ x y)))

;globals
(def x (atom 0)) ;; an atom set to zero
(def y (atom 0)) ;; an atom set to zero
(def data [[0 0], [1 1], [2 2], [3 3]])

;overloaded generator for arithmetics
(defn random-arithmetic
  "Produces, prints, and evals a random arithmetic expression on an atom."
  []
  (reset! x (rand-int 10)) ;;set our atoms to a rand
  (reset! y (rand-int 10))
  (eval '((rand-nth (list + * - div-none)) @x @y))) ;;eval with a rand function

(defn random-arithmetic
  "Produces, prints, and evals a random arithmetic expression on two
passed values."
  [a b]
  (reset! x a) ;;set our atoms to given values
  (reset! y b)
  (eval '((rand-nth (list + * - div-none)) @x @y))) ;;eval with a rand function

(defn random-arithmetic
  "Produces, prints, and evals a random arithmetic expression on two
passed values."
  [a]
  (reset! x a)
  (eval '((rand-nth (list + * - div-none)) @x (rand-int 10)))) ;;eval
with a rand function

;main function
(defn evalrepeat
  "This will find error in data run runnum times."
  [runnum]
  (let [error 0 i 0] ;establish variables for error and a counter
    (repeatedly runnum
                (let [reset! x (get-in data [i 0]) reset! y (get-in
data [i 1])] ;set x, y
                  (inc error (expt (- @y (eval (random-arithmetic
@x))) 2)) ;get error
                  (inc i)))
    error))


More information about the Cs254f11 mailing list