Fibonacci is a bad example because it has a closed-form solution. In J: *fibonacci*=: 3 : 0"0 b=. - a=: % sqrt5=. %: 5 (a * (-:1+sqrt5) ^ y) + b * (-:1-sqrt5) ^ y )
(from my essay "Fibonacci Fun": http://www.jsoftware.com/jwiki/DevonMcCormick/FibonacciFun) Ackermann's function is a better example but this is coded in a recursive manner on the J wiki: ack=: c1`c1`c2`c3 @. (#.@(,&*)) c1=: >:@] NB. if 0=x, 1+y c2=: <:@[ ack 1: NB. if 0=y, (x-1) ack 1 c3=: <:@[ ack [ ack <:@] NB. else, (x-1) ack x ack y-1 (from http://www.jsoftware.com/jwiki/Essays/Ackermann's Function). I also would be interested in seeing this done without recursion as it looks to be so fundamentally recursive that it may be impossible to do it otherwise. On Thu, May 14, 2009 at 2:38 PM, gary ng <[email protected]> wrote: > On Thu, May 14, 2009 at 11:20 AM, Raul Miller <[email protected]> > wrote: > > > > So if by 'serious' you mean coding in 'pure' style(i.e. no fallback to > > > loop), I am curious to know how can one implement algorithms that needs > > > recursion in J. > > > > For example: > > 1 2 3 + 4 > > 5 6 7 > > > This is an example that no recursion is needed. The text book example would > be fibonacci number. Even in language like F# which has TCO, it needs to be > twisted a bit to make it 'TCOtable'. Haskell solution is elegant and simple > due to its laziness not that it has TCO. > > How would that be implemented in J without recursion and while loop ? > ---------------------------------------------------------------------- > For information about J forums see http://www.jsoftware.com/forums.htm > -- Devon McCormick, CFA ^me^ at acm. org is my preferred e-mail ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
