-3

I have never used these two keywords in anything I have programmed because I don't really understand them or when they should be used, if they can be used together etc.

James
  • 161
  • 1
  • 8

1 Answers1

2

Sometimes, you have code like:

foo x y = if min (abs x) (abs y) > 0 then negate (min (abs x) (abs y)) else min (abs x) (abs y)

With let or where, you can introduce abbreviations for common expressions, which makes your code more readable:

foo x y = if z > 0 then negate z else z 
    where 
       z = min (abs x) (abs y)

Which is equivalent to:

 foo x y = let
       z = min (abs x) (abs y)
    in if z > 0 then negate z else z

A let-expression is just that: an expression and can appear anywhere an expression can appear, while a where clause is the optional final part of a case alternative or pattern binding.

Ingo
  • 36,037
  • 5
  • 53
  • 100
  • 2
    There are subtle differences, though. [This](http://www.haskell.org/haskellwiki/Let_vs._Where) link explains it well as the last example where a slight change in how the function is defined can lead to a large difference in performance. – bheklilr Nov 20 '13 at 14:21