0

I am getting this error when I compile:

Ambigous occurence 'map'
It could refer to either Main.map defined at blablabla

I read a similar post here and tried this :

import qualified Data.Map as Map

map                     :: (a->b) -> [a] -> [b]
map f  []               =  []
map f (x:xs)            =  f x : map f xs

I am still getting the error. I am compiling on GHCI.

How can I avoid this ?

5
  • Does adding this at the the beginning of your source solve the problem? import Prelude hiding(map) Commented Nov 8, 2013 at 19:08
  • Oh thanks , it worked ! But what is the difference between my import and yours ? Why to do this ? Commented Nov 8, 2013 at 19:09
  • Prelude is the module which gets imported into every Haskell file implicitly. It also declares a map function and this is triggering the error. By adding the line, you're importing Prelude but hiding the map function in it, thus avoiding the conflict Commented Nov 8, 2013 at 19:12
  • 1
    Perhaps a better solution would be just calling your own implementation differntly, e.g. map' :: (a->b) -> [a] -> [b]. Commented Nov 8, 2013 at 19:23
  • possible duplicate of Ambiguous Occurrence Commented Nov 8, 2013 at 20:00

1 Answer 1

5

You're getting the error because the standard prelude (which is imported by default) alrready has a map function in it.

If you're practising it makes sense to use your own new name for the function. That way you can check yours works the same way as the original. Put a dash' after the name, or call it mymap or something.

You can also do an explicit import so you can leave map out:

import Prelude hiding (map)

but I thinks it's less faf to think of your own non-conflicting name.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.