After reading some literature about subject I can only conclude
solutions like the Haskell example are not really what Amb should do. It
just produces all possibilities at once.

Check out ref. http://tinyurl.com/6g9uzc
See also some discussion in the RC article:
http://www.rosettacode.org/wiki/Talk:Amb


Here's my try for the J example:

initializer/generator:

amb=: 3 : 0
  i_amb_=.1+i_amb_
  if. istop_amb_<:i_amb_ do. '' return. end.
  i_amb_{ambs_amb_
:
  ambs_amb_=. (#~pred"1) ,/>"1,.{"0 1&(,.>y)&.> ,[EMAIL PROTECTED]&.> y
  i_amb_=._1
  istop_amb_=. #ambs_amb_
  LF
)
* Leaving out LF gives an indication if there will be any output :-)


(modified) Rosetta Codes example input:

z1 =: < ;._1 ' the that a'
z2 =: < ;._1 ' frog elephant thing'
z3 =: < ;._1 ' walked treaded grows'
z4 =: < ;._1 ' dumbly slowly quickly'

example run:

condition:

pred=: *./@(2 ({:@>@{. = {.@>@{:) \])

wake up:

   0 amb z1;z2;z3;<z4


interrogation:

   amb''
+---+--------+-------+------+
|the|elephant|treaded|dumbly|
+---+--------+-------+------+
   amb''
+----+-----+-----+------+
|that|thing|grows|slowly|
+----+-----+-----+------+
   amb''


Empty reply on the last interrogation.

Straightforward solution producing all solutions at once is e.g.

joins=: {:@>@{. = {.@>@{:

   (#~*./@(2 joins \])"1),/z1,."0 _ ,/z2 ,."0 _ ,/z3 ,."0 _ z4
+----+--------+-------+------+
|the |elephant|treaded|dumbly|
+----+--------+-------+------+
|that|thing   |grows  |slowly|
+----+--------+-------+------+

 
Another example: values summing to a prime (taken from ref.)

   pred=: 1&p:@({:@>@{. + {.@>@{:)
   0 amb 1 3 5 8;20 35 110


   amb''
3 20
   amb''
3 110
   amb''
8 35
   amb''



Any comments?
e.g. it would be nice initializing incl. pred


=@@i




----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to