We include a let inside a list comprehension much like we would a predicate, only it doesn't filter the list, it only binds to names. Lastly, you can use let in list comprehensions too: calcBmis :: (RealFloat a) => -> Being an expression itself, it cannot be used for binding things for guards. in is an expression itself, thus you can use them wherever you can put an expression. where is just a syntactic construct (not a sugar) that are useful only at function definitions.LHYFGG has a relevant chapter on it -which you should read if you haven't already, but in essence: Sadly, most of the answers here are too technical for a beginner. These two forms of nested scope seem very similar, but remember that a let expression is an expression, whereas a where clause is not - it is part of the syntax of function declarations and case expressions. The same properties and constraints on bindings in let expressions apply to those in where clauses. A where clause is only allowed at the top level of a set of equations or case expression. Note that this cannot be done with a let expression, which only scopes over the expression which it encloses. Is there a general rule of thumb to follow when to use each?įor those that come by this thread later on I found the best explanation to be found here: " A Gentle Introduction to Haskell".Can some apply this to real examples where the variables represent actual expressions?.Is it possible to apply Where to the first example?.In the first example why is the Let in scope but Where is not?.Guards: f | x>0 = 'a' | if expression: f = if x>0 then 'a' else. Pattern matching: f = 0 | case expression: f xs = case xs of -> 0 -Īrguments LHS: f x = x*x | Lambda abstraction: f = \x -> x*x Aside from style how do they perform differently? Declaration style | Expression-style The Haskell wiki mentions that the Where clause is declarative while the Let expression is expressive. Haskell Wiki on Advantages of Let f :: State s a I find its explanations too brief for a beginner. The Haskell Wiki is very detailed and provides various cases but it uses hypothetical examples. In contrast, the scope of a let expression is only the current function clause and guard, if any. The scope of the where clause extends over all guards. The only difference is when guards are being used. Usually, that is identical to the scope of let definition. Could someone please provide some context or perhaps a few examples that demonstrate when to use one over the other?Ī where clause can only be defined at the level of a function definition. Let but I am having trouble discerning when to use each. I have read a few comparisons between Where vs. They both seem to provide a similar purpose. I am new to Haskell and I am very confused by Where vs.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |