[Haskell-beginners] Where/Let clauses and subexpression repetition/memoization

Michael Serra mk.serra at gmail.com
Fri Aug 19 03:21:01 CEST 2011


Dear Haskell Community,

I have been bedeviled by a persistent confusion about one aspect of
where/let clauses, which I think I can illustrate more easily with code than
with words.  Given code like this:

averageR pxls = map (pcast . setR ravg) pxls'
  where (ravg,_,_) = averages pxls'
        pxls'              = map pcast pxls

What happens if I substitute pxls' for its right-hand side,

averageR pxls = map (pcast . setR ravg) (map pcast pxls)
  where (ravg,_,_) = averages (map pcast pxls)

My "superstition" here has been that the former only evaluates pxls' once,
whereas the latter computes it twice.  This seems like a basic issue which
must have been confirmed or debunked somewhere in my readings, but it hasn't
sunk in with me.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20110818/b1957c3c/attachment.htm>


More information about the Beginners mailing list