I am learning Haskell and reading the book, Learn You a Haskell for Great Good!
When the author talks about where
keyword, he says:
In imperative programming languages, you would solve this problem by storing the result of a computation in a variable. In this section, you’ll learn how to use Haskell’s where keyword to store the results of intermediate computations, which provides similar functionality.
However I saw the where
keyword also following at the end of a module declaration, and I doubt the "intermediate computations" explanation in this scenario, what's the meaning of the where followed at the end of the module declaration?
foo = baz
where
baz = 1
quux = 2
...
Compare:
module Foo
where
baz = 1
quux = 2
...
where
is acting as a syntactic introducer of a scope of definitions. However, I believe it is just a trick, for we cannot say:
let baz = 1
quux = 2
in module Foo
or
module Foo
(maybe the latter is legal). I'd like to say that the module
declaration exports (unless otherwise specified) all symbols in scope at the point of declaration; that would be the most consistent. But it's false, so we can consider it at best an idiosyncracy of the concrete syntax. I thought it was weird for a long time too (and upon further reflection answering this question, still do).
At its most basic, where
introduces a new scope. That is its meaning at the top of a module, as well: introduce the scope of module definitions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With