[Haskell-beginners] Can we specify the module of the function being referred

Tillmann Rendel rendel at daimi.au.dk
Tue Oct 14 03:46:26 EDT 2008


Amit Kumar Dhar wrote:
> I don't want to import the 'show' function from prelude. Is there any way
> out or I have to declare instance of the 'Show'?

The prelude is only implicitly imported, if it is not explicitly 
imported. That means that you can explicitly say

   import Prelude hiding (show)

to get rid of Prelude's show.

> Can we specify the module of a function from which it will be referred?

You can give a module a name while importing it, and then use this name 
with a dot. For your situation, maybe the following two import 
statements make sense:

   -- bring everything except show into scope
   import Prelude hiding (show)

   -- make Prelude's show available as P.show
   import qualified Prelude as P

Now you can even use Prelude's show in the definition of your own show. 
Here is an example, showing lists with set notation:

   show :: [Int] -> String
   show xs = "{" ++ content ++ "}" where
     content = concat (intersperse ", " (map P.show xs))

(use "import Data.List" for intersperse).

> I was writing a program which defines a function named "show".

While it is not wrong to use a name from the Prelude for something else, 
it is often confusing, so I would consider using some other name 
(display, toString, prettyPrint, ...).

   Tillmann


More information about the Beginners mailing list