Search code examples
haskelllazy-evaluationghcfoldstrict

Is there a functional representation for strict evaluation?


I want to implement the strict folding functions myself, from within Haskell: Is this possible? I've read that Lisp macros can be used to redefine the language to a massive extent, giving you the power to effectively break out of the functional paradigm whenever you need to and mould it into a personalised paradigm that gets the job done in the neatest way possible. I don't actually know lisp so that may be incorrect.

When you also take into account that in the untyped lambda calculus data types are encoded as functions, I begin to suspect that anything can be encoded as anything else (The brilliant book GEB discusses this in some detail). In that case, representing strict evaluation sounds like it should be easy.

So, how would you go about implementing the following from within haskell?

foldl'  = -- ???
foldl1' = -- ???

I suspect it has something to do with Monads and/or Continuation Passing.


Solution

  • How can you implement foldl'? Like this

    Haskell provides the seq primitive for adding strictness, and "bang patterns" as well for convenience.

    See also: Haskell 2010 > Predefined Types and Classes # Strict Evaluation