[Haskell-cafe] RE: [Haskell] lazy constructors

Simon Peyton-Jones simonpj at microsoft.com
Wed Oct 13 04:09:03 EDT 2004


[I think this thread would be more appropriate on haskell-café, so I'm redirecting it]

| addToSPair :: Char -> (String, String) -> (String, String)
| addToSPair    c       (xs,     ys    ) =  (c:xs,   ys    )
| 
| ---------------------------------------------------------------
| 
| g1 is similar to f1,  g2 similar to f2.
| 
| f1 and f2 are evaluated immediately to 'a';
| 
| g1 evaluates to 'a' after a long computation;
| g2 evaluates to  Fail: <<loop>>
| 
| As g2 is similar to f2, should it have a value 'a' ?
| In
|    head $ fst $ addToSPair 'a' bottomSPair

Try using lazy pattern matching, thus

addToSPair :: Char -> (String, String) -> (String, String)
addToSPair    c       ~(xs,     ys    ) =  (c:xs,   ys    )

The only change is the "~" in the defn of addToSPair.  Now g2 will have value 'a'.

Simon


More information about the Haskell-Cafe mailing list