The names u and v are special, of course:
fold=. {{ y (v F:. u) x}}
u=. '('"_ , [ , ' u '"_ , ] , ')'"_
v=. '('"_ , 'v '"_ , ] , ')'"_
(4 2$'X0X1..XN') u fold v 'Y0'
|stack error
| y (v F:.u)x
But this works, as you probably knew it would:
(4 2$'X0X1..XN') u f.fold (v f.)'Y0'
(v (X0 u Y0))
(v (X1 u (X0 u Y0)))
(v (.. u (X1 u (X0 u Y0))))
(v (XN u (.. u (X1 u (X0 u Y0)))))
Thanks,
--
Raul
On Thu, Mar 4, 2021 at 6:11 PM Jose Mario Quintana
<[email protected]> wrote:
>
> > When I first read about it, I considered it a fun and approachable
> > example – and it lends itself to folding so I support elaborating on
> > this when compiling some learning material/show casing of folds.
>
> I would simplify the example by considering the state to be just the coins
> on the plate. Thus,
>
> knave=. [ , \:~@:] }.~ #@:[
>
> and the state history becomes,
>
> K (knave fold ]) P
> 50 50 50 25 10 5
> 100 25 50 25 10 5
> 5 10 25 25 10 5
> 25 50 10 10 5 5
> 25 10 10 10 5 5
>
> and if one is only interested in the amounts on the plate, then those could
> be produced as follows,
>
> K (knave fold (+/)) P
> 190 215 80 105 65
>
> I often find illustrating the use of symbolic nouns and verbs, for example,
>
> u=. '('"_ , [ , ' u '"_ , ] , ')'"_
> v=. '('"_ , 'v '"_ , ] , ')'"_
>
> 'X' u 'Y'
> (X u Y)
> v 'Y'
> (v Y)
>
> to visualize the products of applied adverbs and conjunctions. Thus, for
> the (swapped) noun arguments,
>
> (4 2$'X0X1..XN') ; 'Y0'
> ┌──┬──┐
> │X0│Y0│
> │X1│ │
> │..│ │
> │XN│ │
> └──┴──┘
>
> the conjunction F:. produces,
>
> 'Y0' v F:. u (4 2$'X0X1..XN')
> (v (X0 u Y0))
> (v (X1 u (X0 u Y0)))
> (v (.. u (X1 u (X0 u Y0))))
> (v (XN u (.. u (X1 u (X0 u Y0)))))
>
> By the way, I tried to show that,
>
> (4 2$'X0X1..XN') u fold v 'Y0'
>
> would produce the same result; but, for some reason, j902 did not comply.
> Oh well...
>
>
> On Fri, Feb 26, 2021 at 2:30 AM Hauke Rehr <[email protected]> wrote:
> >
> > I remember that knaves example.
> > Glad to see you agree x/y are better used the other way around.
> > P is a state altered by each of the knaves in K, respectively.
> > Another good example showing that, if 'control' vs 'data' were
> > a decisive creterion, the order of arguments would need to be changed.
> >
> > When I first read about it, I considered it a fun and approachable
> > example – and it lends itself to folding so I support elaborating on
> > this when compiling some learning material/show casing of folds.
> >
> >
> > Automata are another example: a starting state is altered by
> > successive transitions depending on items of a control input
> > into an accepting state.
> > So replaying a macro (fixed array of commands) takes any current
> > situation (data) and applies the commands one after the other,
> > resulting in the (hopefully intended) final situation.
> >
> > Maybe a “robot in a maze”-like game could show this where
> > users are given a set of macros and some goal to achieve,
> > and can apply the macros until they reach it, macros being
> > replayed by a fold. I’ll add that one to the list of projects
> > I’d hope to get around to implementing. Might even be used
> > for teaching programming, just as the “laby” game does.
> >
> > https://sgimenez.github.io/laby/
> >
> >
> > Am 26.02.21 um 02:33 schrieb Jose Mario Quintana:
> > > Different people may have different preferences, mine is a form that I
> have
> > > used for many years and roughly corresponds to,
> > >
> > > fold=. {{ y (v F:. u) x}}
> > >
> > >
> > > Thus, the example in,
> > >
> > > Loopless Code VI: Temporary Variables (jsoftware.com)
> > > <https://www.jsoftware.com/help/jforc/loopless_code_vi_temporary_v.htm>
> > >
> > > becomes,
> > >
> > > knave=. [ ,~ \:~ @: (] }.~ [: # [)
> > >
> > > ( P=. 100 25 100 50 5 10 )
> > > 100 25 100 50 5 10
> > > ( K =. _2 ]\ 50 50 100 25 5 10 25 50 25 10 )
> > > 50 50
> > > 100 25
> > > 5 10
> > > 25 50
> > > 25 10
> > >
> > > K ([ (knave fold ] ) {:@:[ , ]) P
> > > 100 100 50 25 10 5 50 50
> > > 50 50 50 25 10 5 100 25
> > > 100 50 25 25 10 5 5 10
> > > 25 25 10 10 5 5 25 50
> > > 50 25 10 10 5 5 25 10
> > >
> > > I hope it helps
> > >
> > >
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm