> Paul Graham is collecting canonical accumulator generators at > http://www.paulgraham.com/accgen.html , and has Dylan, E, JavaScript, > various dialects Lisp, Lua, Rebol, Ruby, Perl, Python and Smalltalk.
As others have implied, the only correct answer to this is "it's the wrong question". One of the major advantages of functional programming is that you do most things without global variables, because they are a Bad Thing. So an accumulator based on a variable is simply not the appropriate abstraction in most cases. Think about sum: sum = foldl (+) 0 That works by using an accumulator, but Haskell is so expressive you don't even need to mention the fact! In addition the question is underspecified -- what happens, for example if the argument isn't a number? This is not a problem for Haskell, of course, but in allowing the lisp version to throw an exeption he finesses away the advantage of strong typing. > Could the serious Haskellers comment on this attempt of mine? > > foo n = do > n' <- newIORef n > return (\i -> do { modifyIORef n' (i+); readIORef n' }) What strikes me about this, though, is that perhaps the imperative primitives in Haskell aren't quite perfectly designed. It seems to me that modifyIORef ought to return either the IORef of its value. In Algol68 if n IS a REF INT, (n +:= i) has the value a (which will now contain it's previous contents plus i), so it's a time honoured form. If we had such a version (call it modIORef) we could dazzle the blighters with: foo n = fmap (\n' i -> modIORef n' (+i) >>= readIORef) $ newIORef n and if we had one that returned the value (say modReadIORef) foo n = fmap (\n' i -> modReadIORef n' (+i)) $ newIORef n Now, it was the work of moments to define and test (helped by the type system) the above new functions, which shows what expressive power is really about. Jón PS It's perhaps a bit disappointing that one needs a type signature to use foo = fmap (\n' i->modReadIORef n'(+i)) . newIORef -- Jón Fairbairn [EMAIL PROTECTED] _______________________________________________ Haskell mailing list [EMAIL PROTECTED] http://www.haskell.org/mailman/listinfo/haskell