Hi,
I'm new to core.logic and logic programming in general. I tried to write a
small program to generate all permutations of any size for an input list
with unique elements. To start, I hardcoded the input list to be (range 3).
Here's what I came up with:
(defne everyo [g l]
"A relation that guarantees that g holds for all elements of l."
([_ ()])
([_ [x . r]]
(g x)
(everyo g r)))
(run 16 [q] (everyo #(membero % (range 3)) q) (distincto q))
;; returns (() (0) (1) (2) (0 1) (1 0) (2 0) (0 2) (2 1) (1 2) (2 0 1) (2 1
0) (0 2 1) (1 2 0) (0 1 2) (1 0 2)), as expected
This program works: it returns all 16 permutations of '(0 1 2). However, if
I ask for any more than 16 solutions, the program doesn't terminate. Having
browsed this mailing list a little, I noticed a warning about using
recursive relations first, so I swapped the two clauses of the run clause:
(run 16 [q] (distincto q) (everyo #(membero % (range 3)) q))
;; returns (() [0] [1] [2] (0 0) (0 1) (1 0) (0 2) (1 1) (2 0) (1 2) (2 1)
(2 2) (0 0 0) (0 0 1) (0 1 0)), incorrect
This program returns a different result from the first one. The lists it
returns are no longer distinct, despite declaring that q must be distincto.
Additionally, the singleton lists are vectors for some reason. Asking for
more than 16 results now terminates, but returns more incorrect
(non-distinct) result lists.
Clearly I'm doing something wrong. I suspect that my implementation of
everyo is flawed somehow. Can someone please offer advice?
Thanks,
Jordan Lewis
--
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