On Sun, Apr 18, 2010 at 9:20 PM, verec <
[email protected]> wrote:
> I have two problems with the following code.
> First, I have this `tos' business (to-string) because:
> user=> (first "abc")
> \a
> user=> (rest "abc")
> (\b \c)
> Since I wanted to get strings out of strings, a character or a
> collection is no good.
>
> Second, I ended-up having to use a mutable atom `res' because I
> couldn't figure out how to do it otherwise.
>
> Any pointer on how to tackle either or both problems welcome.
>
> (ns word-play)
>
> (defn rotate
> "Take a collection and left rotates it n steps.
> If n is negative, the collection is rotated right.
> Executes in O(n) time."
> ([coll]
> (rotate 1 coll))
> ([n coll]
> (let [c (count coll)]
> (take c (drop (mod n c) (cycle coll))))))
>
> (defn all-rotations [word]
> "all rots"
> (take (count (str word)) (cycle (iterate rotate (str word)))))
>
> (defn anagram [word]
> "To generate all anagrams of a given string of length n, the most
> obvious solution is to consider all n rotations of the string one
> by one, and for each such rotation, concatenate its first character
> with each of the anagrams of the new string composed of all but its
> first character."
> (cond
> (= (count word) 0) '("")
> (= (count word) 1) (list word)
> true
> (let [all (all-rotations word)
> tos #(str (reduce str %))
> wst (map tos all)
> res (atom "")]
> (doseq [w wst]
> (let [s (str w)
> f (str (first (str s)))
> n (str (tos (next (str s))))
> j #(str f (tos %))
> a (ana3 n)
> r (map j a)]
> (swap! res concat r)))
> @res)))
>
You don't need an atom, look into loop/recur. Also, this can be done in two
lines with the combinatorics clojure-contrib library ;)
user=> (use 'clojure.contrib.combinatorics)
user=> (map #(apply str %) (permutations "lisp"))
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en