15

In one file I need to use the regular prelude (++) operator and I also wish to implement my own behaviour for (++). I have used import Prelude hiding (++) at the top of my file, defined my own (++) operator and now further below I wish to refer to the regular Prelude's (++). How do I achieve this?

Will Ness
  • 70,110
  • 9
  • 98
  • 181
user997112
  • 29,025
  • 43
  • 182
  • 361

2 Answers2

29

Write

import qualified Prelude

in addition to

import Prelude hiding ((++))

at the beginning of the code, and write Prelude.++ where you need ++ in Prelude.

Tsuyoshi Ito
  • 1,288
  • 11
  • 14
4

As Tsuyoshi Ito explained, you can qualify the operator by its module name. However, since by defining your own version of (++) you most likely want to increase the readabilty of your program, qualifying an operator with its module name later on seems to be a weird measure.

Just look at this: "abc" Prelude.++ "def" Now that's ugly.

Why not simply create a new operator, like <++> or an infix function like `append`?

bzn
  • 2,362
  • 1
  • 17
  • 20
  • 3
    Yeah, actually my first reaction to the question was “Do not name your function `(++)`,” although I did not post it. But if the asker is writing a library with its own `++` which is meant to replace the `++` in Prelude, then the user of the library will ideally never have to use `Prelude.++`. In such a (rare) case, it makes sense to define a function with the same name. Otherwise, it makes little sense to cause a name clash with something so prevalent. – Tsuyoshi Ito Nov 06 '11 at 22:57
  • 2
    It is beginning to irritate me when people read past the question and answer with "don't do that"s, especially when the question does not give enough information about its motivation. Let the explorers explore, let them attempt what they think is right, let them see firsthand how ugly it comes out when they do. Soon they will be in a position for me to write this comment on *their* answers. :-) – luqui Nov 08 '11 at 00:32
  • @luqui Lack of information in question is not a good reason to bash the answers. This answer clearly states an alternative. – Pavel Šimerda Aug 30 '21 at 12:32