Howdy, Consider:
infinite =: 1 ; 'last + 2' infinite ┌─┬────────┐ │1│last + 2│ └─┴────────┘ car =: [: > {. car infinite 1 cdr =: monad : 0 last =: car y this =. ". > {: y this ; {: y ) cdr infinite ┌─┬────────┐ │3│last + 2│ └─┴────────┘ cdr cdr infinite ┌─┬────────┐ │5│last + 2│ └─┴────────┘ (monad : 'car cdr^:y infinite')"0 i.10 1 3 5 7 9 11 13 15 17 19 Is this wise, or efficient, or sane, or legal where you are? I've no idea. (Although calling cdr 9 times for the 9th element, and then 10 times for the 10th element, is definitely an efficiency loser. This is a failure of the example rather than the data structure.) But this is an example of an 'infinite list' implemented as a pair of boxed values, the first the first real member of the list, and the second a boxed string describing how further values of the list can be calculated. This is similar to how you might implement a lazy list in a Lisp-like language that doesn't have any native support for them. In most languages you'd use an anonymous closure for the second member of the pair, and an eager language that had some support for 'lazy lists' might have some lazy/force syntax that's closures under the hood. On Wed, 2020-01-08 at 02:24 +0100, Raoul Schorer wrote: > Well, I guess that out of context my question boils down to: > > 'Is it possible to define infinite data structures and operate on > them in > J?' > > For example, take the first ten elements of an infinite list, as in > Haskell. > The 'F.' primitive family seems promising indeed. > > > > Le mer. 8 janv. 2020 à 01:56, Henry Rich <henryhr...@gmail.com> a > écrit : > > > I don't understand the task at all. You might look in NuVoc for > > $:, F. > > etc., and M. > > > > Henry Rich > > > > On 1/7/2020 7:51 PM, Raoul Schorer wrote: > > > Hi, > > > > > > Implementing Minikanren <http://minikanren.org/> in J by > > > translating > > from > > > Scheme, I stumbled on the following: > > > I have a Scheme procedure 'fives' > > > < > > https://github.com/jasonhemann/microKanren/blob/master/microKanren-test-programs.scm > > > that is passed a vector and yields a procedure that then takes a > > > pair. > > > ((fives (vector 0)) '(() . 0)) > > > > > > My problem is that this then evaluates to a pair data -- > > > procedure: > > > ((((#(0) . 5)) . 0) . #<procedure at microKanren-test- > > > programs.scm:677>) > > > > > > The second member of the pair (the procedure) is then evaluated > > > on demand > > > to yield another similar structure of length n+1, therefore > > > allowing the > > > lazy evaluation of this infinite "stream". > > > > > > Is switching to iterative/eager eval the only realistic way? > > > Can someone help me with lazy eval of infinite structures in J, > > > or give > > me > > > pointers to resources on the topic? > > > > > > Thanks! > > > Raoul Schorer > > > --------------------------------------------------------------- > > > ------- > > > For information about J forums see > > > http://www.jsoftware.com/forums.htm > > > > ----------------------------------------------------------------- > > ----- > > For information about J forums see > > http://www.jsoftware.com/forums.htm > > > ------------------------------------------------------------------- > --- > For information about J forums see > http://www.jsoftware.com/forums.htm ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm