I read this:
http://hackage.haskell.org/trac/ghc/wiki/ViewPatterns
I like the idea, want to use the extension. I however would like to make sure as to one thing: whether the view function is evaluated once for a single matching.
So let's say we have:
{-# LANGUAGE ViewPatterns #-}
...
f (view -> Nothing) = ...
f (view -> Just x) = ...
view :: a -> Maybe b
Now let's say I invoke f a
. Is view
invoked twice or just once for the given argument a
?
EDIT:
I tried to find out whether this is the case and wrote the following:
{-# LANGUAGE ViewPatterns #-}
import System.IO.Unsafe
blah (ble -> Nothing) = 123
blah (ble -> Just x) = x
ble x = unsafePerformIO $ do
putStrLn $ "Inside ble: " ++ show x
return x
main :: IO ()
main = do
putStrLn $ "Main: " ++ show (blah $ Just 234)
Output using GHC:
Inside ble: Just 234
Inside ble: Just 234
Main: 234
Output using GHC (with optimization)
Inside ble: Just 234
Main: 234
Output using GHCi:
Main: Inside ble: Just 234
Inside ble: Just 234
234