This sound exactly like what attribute grammars, like the system developed at Utrecht University [1], are useful for.
Erik [1] http://www.cs.uu.nl/wiki/HUT/AttributeGrammarSystem On Fri, Jul 1, 2011 at 10:54, Eugene Kirpichov <ekirpic...@gmail.com> wrote: > Alexey, your definition of "mean" does not look like "liftS2 (/) sum > length" - you have to manually "fuse" these computations. > > I'm asking for a formalism that does this fusion automatically (and > guaranteedly). > > 2011/7/1 Alexey Khudyakov <alexey.sklad...@gmail.com>: >> On Fri, Jul 1, 2011 at 12:21 PM, Eugene Kirpichov <ekirpic...@gmail.com> >> wrote: >>> I meant the average of the whole list - given a sumS and lengthS ("S" >>> for "Stream"), write meanS as something like liftS2 (/) sumS lengthS. >>> >>> Or is that possible with lazy lists too? >>> >> Sure you can. Sum, length and mean could be calculated as left >> fold. If you need to calculate more that one statistic at time you >> can combine accumulators >> >>> sum = foldl (+) 0 >>> length = foldl (\n _ -> n+1) 0 >>> data Mean Double Int >>> mean = foldl (\(Mean m n) x -> Mean (m + (x - m) / fromIntegral (n+1)) >>> (n+1)) (Mean 0 0) >> >> AFAIU iteratees basically use same technique. >> > > > > -- > Eugene Kirpichov > Principal Engineer, Mirantis Inc. http://www.mirantis.com/ > Editor, http://fprog.ru/ > > _______________________________________________ > Haskell-Cafe mailing list > Haskell-Cafe@haskell.org > http://www.haskell.org/mailman/listinfo/haskell-cafe > _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe