0
votes

I am trying to solve riddle similar to the Einstein's riddle. To solve this I am using library for making permutations

First I define all permutations over subjects

(def auta (permutations [:Opel :Jeep :Renault :Octavia :Peaguot]))
(def skym (permutations [:pes :rodina :kamaradka :pratele :sam]))
(def mena (permutations [:Honza :Viktor :Marek :Ota :pavel]))
(def prace (permutations [:Student :Malir :Technik :Prog :nic]))
(def typ  (permutations [:Wherti :Myst :Multi :trad :earth]))

Next I make the list of matrix of solutions

(def all (for 
[a (doall auta) s (doall skym) m (doall mena) p (doall prace) t(doall typ)]
[a s m p t]))

This working as I want and I can print first item

(print (take 1 all))

This will show me:

([(:Opel :Jeep :Renault :Octavia :Peaguot) (:pes :rodina :kamaradka :pratele :sa
m) (:Honza :Viktor :Marek :Ota :pavel) (:Student :Malir :Technik :Prog :nic) (:W
herti :Myst :Multi :trad :earth)])nil

Now I want to add a conditions in for (at first I want to add condition that the :pavel is first name of permutation.

(def all (for 
[a (doall auta) s (doall skym) m (doall mena) p (doall prace) t(doall typ)
:when (= (m 1) :pavel) ]
[a s m p t]))

and I end with

CompilerException java.lang.ClassCastException: clojure.lang.LazySeq cannot be c
ast to clojure.lang.IFn, compiling:(C:\Users...

What I need t change in condition to make it running?

P.S.: I try to do thing like (doall m) etc. but it did not help.

1

1 Answers

2
votes

The problem is that permutations returns a seq and you're trying to access the first element of that seq using (m 1) (which, btw, is also incorrect since indices start at 0). This, however, only works for vectors:

(def sq (range 1 5))
(def v  (vec sq))

(sq 1) ; => ClassCastException clojure.lang.LazySeq cannot ...
(v 1)  ; => 2
(v 0)  ; => 1 (this is the actual first element)

Solution: use nth:

(nth sq 0) ; => 1
(nth v 0)  ; => 1