On Nov 17, 2007 6:42 AM, Edushka S. <[EMAIL PROTECTED]> wrote:
> As an example, for the time being I am convinced, that the following
> Haskell implementation of "one pass" maximum consecutive sum algorithm
> via foldr:
>
> foldr (\x (csum,csummin,cresult) -> let nsum=x+csum ; nmin=min csummin
> nsum ; in (nsum,nmin,max cresult (nsum-nmin)) ) (0,0,0) [0, 4, 1, -2,
> 1, -5, 4, -3, 5, -2, -1, 2, 6, -2, 1, -1]
> (8,-2,11)
>
> cannot be implemented in J using only Insert without, as an option,
> preliminary boxing as in my implementation of "more generic" foldr
> above.
csum=: [EMAIL PROTECTED]
csummin=: [EMAIL PROTECTED]@]
cresult=: {:@]
nsum=: [ + csum f.
nmin=: nsum <. csummin f. <. nsum
F=: (nsum, nmin, cresult f. >. nsum-nmin)/@,&0
F 0 4 1 _2 1 _5 4 _3 5 _2 _1 2 6 _2 1 _1
8 _2 11
(I have marked (with f.) the use of names which I am ambivalent about
-- I am not
sure including them adds clarity to the presentation. For example, note that
each is referenced exactly once.)
--
Raul
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm