[Cs254f11] Another Q

Lee Spector lspector at hampshire.edu
Wed Sep 28 12:53:43 EDT 2011


More answers:

The set intersection/difference/etc stuff becomes available by doing:

(use 'clojure.set)

Auto-indenting also showed me something wrong with your compare-xy, because the bulk of the definition should be within the "let" but I can see when I auto-indent that the print statements are directly under the "let", not indented as they should be. The problem was a stray ")" after the "]" that ends the "let" bindings.

Two other small problems: 

- the set theory instructions only work on sets, but rand-sub returns a list. Wrap (set ...) around it and that's fixed.

- the "difference" function doesn't have an "s" at the end.

So this works (although you might want to tweak the output format):


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

 -Lee


On Sep 28, 2011, at 12:41 PM, Lee Spector wrote:

> 
> Partial answer for now (I'm in a meeting):
> 
> Your second argument to concat is, as you note, a string returned by rand-nth (which is given a list of strings, and that's why it is returning a string).
> 
> Since concat is a general sequence function it is treating that string argument as a sequence of characters, and that's why you're seeing the list of characters.
> 
> So to fix that you want to replace (rand-nth fav) with (list (rand-nth fav)), so that you have a list containing a string, and what you want to concat together are lists of strings, not strings themselves.
> 
> There's another error that I saw immediately once I added some newlines and auto-re-indented:
> 
> (defn rand-sub
> [fav]
> (loop [i 0 temp (list)]
>   (if (= i (count fav))
>     temp
>     (recur (inc i)
>            (if (< 3 (rand-int 10))
>              (concat temp 
>                      (list (rand-nth fav))))))))
> 
> (This may look better if you paste it into clooj or something else with a monospace font... depending on what you use to read mail.)
> 
> Do you see the problem?
> 
> The "if" has a "then" clause but not an "else" clause. So if the condition is true then this expression will return (and pass to the top of the loop as the next value of temp) the result of the call to concat. But if it's FALSE then the "if" expression will return (and pass to the top of the loop) nil, which isn't what you want -- in that case you want to pass temp itself (with nothing concatenated to it):
> 
> (defn rand-sub
> [fav]
> (loop [i 0 temp (list)]
>   (if (= i (count fav))
>     temp
>     (recur (inc i)
>            (if (< 3 (rand-int 10))
>              (concat temp 
>                      (list (rand-nth fav)))
>              temp)))))
> 
> -Lee
> 
> On Sep 28, 2011, at 11:59 AM, James Matheson wrote:
> 
>> 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.
>> _______________________________________________
>> Cs254f11 mailing list
>> Cs254f11 at lists.hampshire.edu
>> https://lists.hampshire.edu/mailman/listinfo/cs254f11
> 
> --
> Lee Spector, Professor of Computer Science
> Cognitive Science, Hampshire College
> 893 West Street, Amherst, MA 01002-3359
> lspector at hampshire.edu, http://hampshire.edu/lspector/
> Phone: 413-559-5352, Fax: 413-559-5438
> 
> _______________________________________________
> Cs254f11 mailing list
> Cs254f11 at lists.hampshire.edu
> https://lists.hampshire.edu/mailman/listinfo/cs254f11

--
Lee Spector, Professor of Computer Science
Cognitive Science, Hampshire College
893 West Street, Amherst, MA 01002-3359
lspector at hampshire.edu, http://hampshire.edu/lspector/
Phone: 413-559-5352, Fax: 413-559-5438



More information about the Cs254f11 mailing list