Haskell pattern matching match Map.empty any with any Map.Map k v -
i writing function has different operations depending on map.map in argument if map empty or not
here code:
import qualified data.map.lazy map testf :: (ord a, num a) => map.map a -> [a] -> [a] testf empty _ = [1024] testf _ [] = [] testf m (x:xs) = [x] ++ (testf m xs) main = let = map.fromlist [(1,2), (3,4)] print $ testf map.empty [1,2,3] -- show [1024] print $ testf [1,2,3] -- show [1024] print $ == map.empty -- false of course ghc give me notification lastest line of function redundant.
pattern match redundant in equation ‘testf’: testf m (x : xs) = - why every map.map can match map.empty?
- what should implement requirement?
thank you.
you're not matching against map.empty, you're matching against empty, local variable bound whatever map coming in. can't match against map.empty because it's not constructor.
what can instead is:
testf m _ | map.null m = [1024] i.e. use guard check whether m empty.
Comments
Post a Comment