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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With