Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell: How can I use "2 functions with the same name"?

Tags:

haskell

Haskell: how to use the function have the same name but belongs to different package?

This is my code

 insert a = a
insert2 a = Data.List.insert 4 [1,3,5,7,9]

The error is :

not in scope: data constructor 'Data.List'.

Even i change it to

Data.List::insert 4 [1,3,5,7,9]   --the error still exists

How can I fix it.

like image 928
user3239558 Avatar asked Jan 29 '14 23:01

user3239558


1 Answers

You almost had it:

Data.List.insert 4 [1, 3, 5, 7, 9]

And

Main.insert 4 [1, 3, 5, 7, 9]
-- or if not in Main
Full.Qualified.CurrentPackage.insert 4 [1, 3, 5, 7, 9]

But you have to import the package first. I would recommend

import qualified Data.List
-- or
import qualified Data.List as L

If you use the second form you can just do

L.insert 4 [1, 3, 5, 7, 9]

As a full example, your file could look like

module Main where

import qualified Data.List
import qualified Data.List as L

insert x xs = undefined  -- Fill in your implementation here

main = do
    print $ insert 4 [1, 3, 5, 7, 9]
    print $ Main.insert 4 [1, 3, 5, 7, 9]
    print $ Data.List.insert 4 [1, 3, 5, 7, 9]
    print $ L.insert 4 [1, 3, 5, 7, 9]

All of these would work.

like image 89
bheklilr Avatar answered Sep 27 '22 18:09

bheklilr