Reputation: 1437
In the following code I had to use elem'
instead of simply elem
, because prelude already has a function elem
, how could I use elem
while declaring Tree module, avoiding conflicts??
module Tree(Tree(..), singleton, insert, elem') where
data Tree a = Empty | Node a (Tree a) (Tree a) deriving (Show)
singleton :: a -> Tree a
singleton a = Node a Empty Empty
insert :: (Ord a) => a -> Tree a -> Tree a
insert e Empty = singleton e
insert e tree@(Node e2 left right)
| e == e2 = tree
| e > e2 = Node e2 left (insert e right)
| e < e2 = Node e2 (insert e left) right
elem' :: (Ord a) => a -> Tree a -> Bool
e `elem'` Empty = False
e `elem'` (Node e2 left right)
| e == e2 = True
| e > e2 = e `elem'` right
| e < e2 = e `elem'` left
Upvotes: 2
Views: 992
Reputation: 36612
While the Prelude
module is in scope by default, this can be overridden. Any module that does not explicitly import Prelude
has an implicit import Prelude
at the start; however, if you explicitly import the Prelude, then this isn't added. Thus, you can import Prelude
except for elem
by using hiding
:
import Prelude hiding (elem)
In the context of your code, this is
module Tree(Tree(..), singleton, insert, elem) where
import Prelude hiding (elem)
data Tree a = Empty | Node a (Tree a) (Tree a) deriving (Show)
-- ...
elem :: (Ord a) => a -> Tree a -> Bool
e `elem` Empty = False
e `elem` (Node e2 left right)
| e == e2 = True
| e > e2 = e `elem` right
| e < e2 = e `elem` left
If you still need to refer to Prelude.elem
, you can always add an extra import qualified Prelude
line, too.
For more on module imports, see the Haskell Wiki's nice summary of import
, including the various options for how to import modules and a bit about (suppressing the) implicit Prelude
import.
(Of course, although you're generally unlikely to need it, the final arbiter for modules is the Haskell 2010 report, Ch. 5; §5.3 is on import syntax, §5.5 is on name conflicts, and §5.6 is on the Prelude
(including §5.6.2, "Shadowing Prelude Names").
Upvotes: 5