On Sun, Nov 13, 2011 at 10:46 AM, Jules <julesjac...@gmail.com> wrote:

> Here is a slight modification of the previous program that can be
> executed in two directions:
>
> N = [40]
> A = range(1,40)
> B = range(1,40)
> C = range(1,40)
> D = range(1,40)
>
> def valid(a,b,c,d,n):
>     weights = set(w*a+x*b+y*c+z*d for w in [-1,0,1]
>                                  for x in [-1,0,1]
>                                  for y in [-1,0,1]
>                                  for z in [-1,0,1])
>     return weights >= set(range(1,n+1))
>
> ws = [(a,b,c,d,n) for n in N
>                  for a in A
>                  for b in B if a <= b
>                  for c in C if b <= c
>                  for d in D if c <= d
>                  if a+b+c+d == n and valid(a,b,c,d,n)]
>
> Change the variable domains to execute it in the other direction:
>
> N = range(1,100)
> A = [1]
> B = [3]
> C = [9]
> D = [27]
>
> This produces the single answer (1,3,9,27,40), i.e. the only solution
> is with n = 40.
>

That's very nice! But of you're not providing any sort of generic interface
to do declarative programming, which is the whole point of cKanren. Also
since you're expressing your constraints with for comprehensions you need
to order them correctly, and so on.

What I mean by cheating is that you hard-code part of the solution.
> Would you consider the program `print "1,3,9,27"` a good solution? To
> me at least, it's non obvious that the solution would have to have a=1
> and would have to have a,b,c,d all different. Sure, for the way you
> interpreted the problem originally, a=1 is obvious. But since now you
> can subtract weights as well as adding them it is not obvious (to me).
> Why couldn't a=3 and b=4 work? Then you can still weigh 1lbs. If you
> consider it obvious, can you explain?


My line of reasoning

- let's assume we weigh 1 with two stones, neither of which are 1. Imagine
we can weigh 40. It's impossible to weigh 39.
- having two stones of the same weight is useless
- we might as well order them by weight to remove useless permutations

David

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to