Hi Mike,

I don't fully follow your example because it never returns a value that you
are searching for.  Something like this would seem to make more sense to me:

def bk(w)
  if illegal(w) return null
  if goal(w) return w
  a = bk(w+'a'); if a != null then return a
  b = bk(w+'b'); if b != null then return b
  c = bk(w+'c'); if c != null then return c
end
bk('')

The above also doesn't jive all that well with "searching for the longest
string" which seems like a potentially infinite endeavour.

I have attached some code I quickly hacked up for doing arc consistency
search.  There's a lot more context there but you are interested in the
'search' function.  Maybe someone else can shed light on how to pass around
and apply verbs as arguments.  Combining the two would give you what you're
looking for I think.

Like Raul said, this isn't the use case where J shines unless the
computations denoted by illegal(), goal() or +'a' are significant.

Cheers,

Mike




On Wed, Jun 26, 2013 at 1:37 AM, Mike Müller <[email protected]> wrote:

> Hello!
>
> I hope this is not too simple a question for this list, but I'm still
> learning J and wonder how to do a simple backtracking in J.
> For instance, I might want to see what is the longest string consisting of
> a,b, and c, that fulfills some property P.
> In any imperative language (here: ruby) I would do something like this…
>
> def bk(w)
>    if (!P(w)) return
>    bk(w+"a")
>    bk(w+"b")
>    bk(w+"c")
> end
> bk("")
>
> …and combine it with some bookkeeping of which was the longest that I've
> seen so far and maybe some output every 100 steps,
> if there is actually no longest one (that is, there is an infinite one
> fulfilling P).
>
> I'm very interested how to do these kind of things elegantly in J.
>
> Best regards,
> Mike
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to