Actually, data model is different. The APL2 example
uses nested boxes to emulate LIPS lists. Here's such
structures in J. Each necting is Question;YesChoice;NoChoice,
Choice is either animal of another nesting.
Path in such binary tree is 0 for Yes and 1 for No.
Readable representation of nested structures with stacked list.
Emulates a pseudo-unfetch }:: for binary tree.
This does not have the play, just the structure and operations.
NB. =========================================================
NB. animals_box.ijs
require 'strings'
pop=: 3 : 'z [ ST=: }:ST [ z=. {:ST'
popall=: 3 : 'ST=: 0 3$0'
push=: 3 : 'ST=: ST,y'
BASE=: 0 : 0 NB. stacked list
woods ? wolf : dog
purr ? cat : mouse
bark ? . : .
web ? spider : fish
fur ? . : .
)
gen=: 3 : 0
'Q Y N'=. y [ r=. ''
if. L.Y do. Y=. '.' [ r=. r,gen Y end.
if. L.N do. N=. '.' [ r=. r,gen N end.
r,Q,' ? ',Y,' : ',N,LF
)
parseline=: [: deb&.> [: <;._2 [: ,&'?' '?' [`(i.&':'@])`]} ]
parse=: 3 : 0
popall ''
([: push [: pop^:((,'.')&-:)&.>&.|. parseline);._2 y
{.ST
)
cascade=: 3 : 'for_i. >:i.L.y do. y=.,.^:(0<L.)L:i y end.'
unfetch=: 1 : 0 NB. pseudo-unfetch: binary tree case
:
if. (0=L.y)+.0=#m do. x return. end.
r=. {.y
if. -.{.m do. r=. r,<x (}.m) amend 1{::y else. r=. r,1{y end.
if. {.m do. r=. r,<x (}.m) amend 2{::y else. r=. r,2{y end.
r
)
0 : 0
parse BASE
gen parse BASE
cascade parse BASE
parseline;._2 BASE
;L:1{::parse BASE
('fish';'fisherman';'spider') 1 0 unfetch parse BASE
gen ('dam';'beaver';'mouse') 0 1 1 unfetch parse BASE
)
NB. =========================================================
parse BASE
+---+----------------------------------------+-----------------+
|fur|+----+----------------+----------------+|+---+------+----+|
| ||bark|+-----+----+---+|+----+---+-----+|||web|spider|fish||
| || ||woods|wolf|dog|||purr|cat|mouse|||+---+------+----+|
| || |+-----+----+---+|+----+---+-----+|| |
| |+----+----------------+----------------+| |
+---+----------------------------------------+-----------------+
gen parse BASE
woods ? wolf : dog
purr ? cat : mouse
bark ? . : .
web ? spider : fish
fur ? . : .
cascade parse BASE
+---------+
|fur |
+---------+
|+-------+|
||bark ||
|+-------+|
||+-----+||
|||woods|||
||+-----+||
|||wolf |||
||+-----+||
|||dog |||
||+-----+||
|+-------+|
||+-----+||
|||purr |||
||+-----+||
|||cat |||
||+-----+||
|||mouse|||
||+-----+||
|+-------+|
+---------+
|+------+ |
||web | |
|+------+ |
||spider| |
|+------+ |
||fish | |
|+------+ |
+---------+
parseline;._2 BASE
+-----+------+-----+
|woods|wolf |dog |
+-----+------+-----+
|purr |cat |mouse|
+-----+------+-----+
|bark |. |. |
+-----+------+-----+
|web |spider|fish |
+-----+------+-----+
|fur |. |. |
+-----+------+-----+
;L:1{::parse BASE
+-+---------------------------------------------+-------------+
|0|+---+-------------------+-------------------+|+---+---+---+|
| ||1 0|+-----+-----+-----+|+-----+-----+-----+|||2 0|2 1|2 2||
| || ||1 1 0|1 1 1|1 1 2|||1 2 0|1 2 1|1 2 2|||+---+---+---+|
| || |+-----+-----+-----+|+-----+-----+-----+|| |
| |+---+-------------------+-------------------+| |
+-+---------------------------------------------+-------------+
('fish';'fisherman';'spider') 1 0 unfetch parse BASE
+---+----------------------------------------+----------------------------------+
|fur|+----+----------------+----------------+|+---+-----------------------+----+|
|
||bark|+-----+----+---+|+----+---+-----+|||web|+----+---------+------+|fish||
| || ||woods|wolf|dog|||purr|cat|mouse|||| ||fish|fisherman|spider||
||
| || |+-----+----+---+|+----+---+-----+||| |+----+---------+------+|
||
|
|+----+----------------+----------------+|+---+-----------------------+----+|
+---+----------------------------------------+----------------------------------+
gen ('dam';'beaver';'mouse') 0 1 1 unfetch parse BASE
woods ? wolf : dog
dam ? beaver : mouse
purr ? cat : .
bark ? . : .
web ? spider : fish
fur ? . : .
--- Oleg Kobchenko <[EMAIL PROTECTED]> wrote:
> I also have an implementation with a very similar data model as below,
> plus it's simplified both in memory boxes and in file storage.
>
...
> --- June Kim <[EMAIL PROTECTED]> wrote:
>
> > Thanks. I'll study your code.
> >
> > The APL2 implementation I read was done with nested boxes. At the top
> > level there are three boxes : initial question, yes-animal, no-animal.
> > When the question has to be extended, the no-animal element is filled
> > with another size-three tuple(three boxes). Therefore, the knowledge
> > base is represented as nested boxes. Is this common in J, representing
> > trees?
> >
> > BTW, for those who are interested in this game, have a look at other
> > implementations and some explanations I gathered:
> ...
>
> __________________________________________________
> Do You Yahoo!?
> Tired of spam? Yahoo! Mail has the best spam protection around
> http://mail.yahoo.com
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm