
There seem to be a few situations where it's not clear to me when to use "let" and when "where". For instance, in this little example I was playing with to work out what syntax works, main = putStr (show (if maybe_index == Nothing then DP_Unknown else DP_Number index) ++ "\n") where maybe_index = maybe_read word (Just index) = maybe_index or... main = let maybe_index = maybe_read word (Just index) = maybe_index in putStr (show (if maybe_index == Nothing then DP_Unknown else DP_Number index) ++ "\n") Does anyone care? At the moment I use "where" so that at a first glance you get an overall idea of things, then you can read further for details if you like. I was disappointed to find that I don't seem to be able to write things like, main = let maybe_index = maybe_read word in putStr (show (if maybe_index == Nothing then DP_Unknown else DP_Number index) ++ "\n") where (Just index) = maybe_index BTW, is the above a sane way of getting the 'index' 'out of' the "Just"? I often seem to be using a "where (Just foo) = bar" type of idiom. (Of course, there is that special "let" stuff for "do" notation too which doesn't seem to use "in".) I hope all that was somewhat coherent, anyway, or at least sheds light on some of the confusion of newcomers to Haskell! -- Mark

On Wed, Sep 19, 2001 at 01:53:22PM -0400, Mark Carroll wrote:
main = let maybe_index = maybe_read word in putStr (show (if maybe_index == Nothing then DP_Unknown else DP_Number index) ++ "\n") where (Just index) = maybe_index
BTW, is the above a sane way of getting the 'index' 'out of' the "Just"? I often seem to be using a "where (Just foo) = bar" type of idiom.
I'd probably write this as something like main = case maybe_read word of Just index -> putStrLn $ show $ if maybe_index == Nothing then DP_Unknown else DP_Number index Nothing -> error "No index found" As for let versus where, I use whatever I think looks best, which tends to be where unless I have lots of nested ifs and cases. Ian

Yes, Haskell is a rather big language and unfortunately has much redundancy. It seems mostly to be a matter of taste which of `let' and `where' you prefer. Personally, I nearly always use `where' when possible (for the same reason you give); `let' only if the `where' would be too far away. However, for your example I would use `case': main = putStrLn . show $ case maybe_read word of Nothing -> DP_Unkown Just index -> DP_Number index (Another matter of tast: I don't like `_' in identifiers, but use capital letters like the Haskell Prelude)
I was disappointed to find that I don't seem to be able to write things like,
main = let maybe_index = maybe_read word in putStr (show (if maybe_index == Nothing then DP_Unknown else DP_Number index) ++ "\n") where (Just index) = maybe_index
No, the `let' is in the scope of the `where', but the `where' is not in the scope of the `let'. Expressions are nested and allowing mutual recursion here would make things really confusing. Ciao, Olaf -- OLAF CHITIL, Dept. of Computer Science, University of York, York YO10 5DD, UK. URL: http://www.cs.york.ac.uk/~olaf/ Tel: +44 1904 434756; Fax: +44 1904 432767
participants (3)
-
Ian Lynagh
-
Mark Carroll
-
Olaf Chitil