Tutorial by Examples

If we have a file called Business.hs, we can define a Business module that can be import-ed, like so: module Business ( Person (..), -- ^ Export the Person type and all its constructors and field names employees -- ^ Export the employees function ) where -- begin types, function defin...
To export the type and all its constructors, one must use the following syntax: module X (Person (..)) where So, for the following top-level definitions in a file called People.hs: data Person = Friend String | Foe deriving (Show, Eq, Ord) isFoe Foe = True isFoe _ = False This module d...
Haskell supports importing a subset of items from a module. import qualified Data.Stream (map) as D would only import map from Data.Stream, and calls to this function would require D.: D.map odd [1..] otherwise the compiler will try to use Prelude's map function.
Prelude often defines functions whose names are used elsewhere. Not hiding such imports (or using qualified imports where clashes occur) will cause compilation errors. Data.Stream defines functions named map, head and tail which normally clashes with those defined in Prelude. We can hide those impo...
When multiple modules define the same functions by name, the compiler will complain. In such cases (or to improve readability), we can use a qualified import: import qualified Data.Stream as D Now we can prevent ambiguity compiler errors when we use map, which is defined in Prelude and Data.Stre...
The names of modules follow the filesystem's hierarchical structure. With the following file layout: Foo/ ├── Baz/ │ └── Quux.hs └── Bar.hs Foo.hs Bar.hs the module headers would look like this: -- file Foo.hs module Foo where -- file Bar.hs module Bar where -- file Foo/Bar.hs m...

Page 1 of 1