[Cs254f11] Another Q

James Matheson jmatheson12 at gmail.com
Wed Sep 28 11:59:51 EDT 2011


Hi guys. Another q here (as I slowly make my way through the Problem set...)

This is for making two random lists which are then compared. For some
reason rand-sub returns a list of the chars that I want, not a list of
the strings. I've looked at rand-nth (which returns a string) and temp
is a list which is made with rand-nth, so that ought to be made of
strings... I've tried casting (str) onto rand-nth without any
change... I think there must be something wrong with my loop
conditions. Any ideas?

Thanks in advance,
Jamie

(def fav '["love" "money" "knowledge"])

(defn rand-sub
  [fav]
  (loop [i 0 temp (list)]
    (if (= i (count fav)) ;;want to go through each value in list;
count fav evals to 3
      temp
      (recur(inc i)(if(< 3 (rand-int 10)) (concat temp (rand-nth fav)))))))

(defn compare-xy
  []
  (let [x (rand-sub fav) y (rand-sub fav)])
  (print (intersection x y))
  (print (differences x y))
  (if (= x y)
    (print "The subsets are equal")))

(rand-sub fav)

;;P.S. Does my compare-xy need a certain namespace to work? I remember
Lee saying that intersection and differences were within a certain
library.


More information about the Cs254f11 mailing list