Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to fix Ambiguous occurrence error in Haskell

I have wrote one function which has a signature of

sort :: [Int] -> [Int]

which gives me an error of

Ambiguous occurrence ‘sort’

I know there is already a built-in function called sort in

import Data.List

How can i fix this issue while keeping the same type signature?

like image 471
USERSFU Avatar asked Feb 05 '23 14:02

USERSFU


2 Answers

You can try

import Data.List hiding (sort)

This will prevent Data.List.sort from being imported, leaving you free to define your own function called sort.

If you want to be able to use Data.List.sort in addition to your own, also add the line

import qualified Data.List

or

import qualified Data.List as L

This allows you to to access the library function as Data.List.sort or L.sort, respectively.

like image 77
melpomene Avatar answered Feb 08 '23 16:02

melpomene


Try qualifying the name:

module Foo where

import Data.List as L

Then, to refer to the library sort, use L.sort. For the user-defined sort, use Foo.sort instead.

like image 34
chi Avatar answered Feb 08 '23 15:02

chi