Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

meaning of where keyword in module declaration

Tags:

haskell

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?

like image 975
Sawyer Avatar asked Sep 07 '11 23:09

Sawyer


2 Answers

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).

like image 172
luqui Avatar answered Sep 27 '22 18:09

luqui


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.

like image 23
Daniel Wagner Avatar answered Sep 27 '22 18:09

Daniel Wagner