[Haskell] Probably a trivial thing for people knowing Haskell

Brandon S. Allbery KF8NH allbery at ece.cmu.edu
Sun Oct 19 11:43:09 EDT 2008


On 2008 Oct 19, at 11:25, Friedrich wrote:
> Ok to  be more concrete is the laziness "hidden" here?
>
> check_line line sum count =
>    let match = matchRegex regexp line
>        in case match of
>               Just strs -> (sum + read (head strs) :: Integer, count  
> + 1)
>               Nothing -> (sum, count)


For starters, "let" is lazy.  "case" is strict to the extent that it  
has to evaluate enough to decide if the result is Just or Nothing, but  
that's useless if the code leading up to its application is lazy.  I  
don't know how lazy matchRegex is, if it is lazy enough then all that  
gets evaluated by the case is just enough to know if the result is  
Just or Nothing but not the value of "strs".  (One obvious possibility  
is that it determines that there *are* strings, but not what they  
are.)  Everything else is automatically lazy.

So, unless the caller forces the result of this function, you are very  
likely to end up with a chain of partially evaluated matches that  
don't get resolved until the result is printed.

-- 
brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery at kf8nh.com
system administrator [openafs,heimdal,too many hats] allbery at ece.cmu.edu
electrical and computer engineering, carnegie mellon university    KF8NH




More information about the Haskell mailing list