Reputation: 815
I am writing a function which has different operations depending on Map.Map
in argument if Map is empty or not
Here is my code:
import qualified Data.Map.Lazy as Map
testF :: (Ord a, Num a) => Map.Map a a -> [a] -> [a]
testF empty _ = [1024]
testF _ [] = []
testF m (x:xs) = [x] ++ (testF m xs)
main = do
let a = Map.fromList [(1,2), (3,4)]
print $ testF Map.empty [1,2,3] -- show [1024]
print $ testF a [1,2,3] -- show [1024] too
print $ a == Map.empty -- False
Of course GHC already give me notification that lastest line of function is redundant.
Pattern match is redundant
In an equation for ‘testF’: testF m (x : xs) =
Thank you.
Upvotes: 1
Views: 1858
Reputation: 85827
You're not matching against Map.empty
, you're matching against empty
, which is just a local variable bound to whatever map is coming in. You can't match against Map.empty
because it's not a constructor.
What you can do instead is:
testF m _ | Map.null m = [1024]
I.e. use a guard to check whether m
is empty.
Upvotes: 7