[Haskell-cafe] A probably-stupid question about a
Prelude implementation.
Jan-Willem Maessen
jmaessen at alum.mit.edu
Fri Jun 22 16:31:01 EDT 2007
On Jun 22, 2007, at 2:30 PM, Dan Weston wrote:
> This is how I think of it:
>
> lazyIntMult :: Int -> Int -> Int
> lazyIntMult 0 _ = 0
> lazyIntMult _ 0 = 0
> lazyIntMult a b = a * b
>
> *Q> 0 * (5 `div` 0)
> *** Exception: divide by zero
> *Q> 0 `lazyIntMult` (5 `div` 0)
> 0
>
> foldr evaluates a `f` (b `f` (c `f` ...))
>
> Only f knows which arguments are strict and in which order to
> evaluate them. foldr knows nothing about evaluation order.
And, indeed, if you foldr a function with left zeroes, and you check
for them explicitly as lazyIntMult and (||) do, then foldr is
guaranteed to terminate early if it finds a zero.
z is a left zero of op if for all x, z `op` x = z.
This isn't the only time foldr will terminate early, but it is an
important one.
-Jan-Willem Maessen
>
> Dan
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2425 bytes
Desc: not available
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20070622/3270acc2/smime.bin
More information about the Haskell-Cafe
mailing list