Sir Hadez
Sir Hadez

Reputation: 23

Haskell module not compiling

So imagine if i have this module declaration

module MyModule
(FavoriteList
,empty
,insert
,delete
)where
  data FavoriteList a = L [a]
empty :: FavoriteList a
empty = FavoriteList []

For some reason that surpasses my experience in Haskell this does not compile, sometimes it says that the Data constructor is not in scope, and when i move it in the code it says that exists a parse error in input on the empty function. Any help?

EDIT:

)where
  data FavoriteList a = L [a]
  empty :: FavoriteList a
  empty = FavoriteList []

Upvotes: 0

Views: 94

Answers (2)

bheklilr
bheklilr

Reputation: 54068

Your code is poorly formatted and you need to follow whitespace conventions properly, since the Haskell compiler is whitespace aware. Something like this should work:

module MyModule
    ( FavoriteList(..)
    , empty
    ) where

data FavoriteList a = L [a]

empty :: FavoriteList a
empty = L []

You should use the form FavoriteList(..) to export the type and all of its constructors, and the constructor for FavoriteList is L, not FavoriteList, which is the type name, so you have to construct a value of type FavoriteList using the L constructor.

All top level declarations must be at the same indentation level, too, so you can't have

    data FavoriteList a = L [a]

empty :: FavoriteList a
empty = L []

You have to have

data FavoriteList a = L [a]

empty :: FavoriteList a
empty = L []

Upvotes: 5

Daniel Wagner
Daniel Wagner

Reputation: 153172

Since where is a block herald, all lines after it must be indented the same amount (or explicit {;} must be used).

Upvotes: 2

Related Questions