Hello I am learning Haskell at the moment and I was wondering if and how I can pass a function of Eq as a parameter to another function since it works with ( ), (-) and (*) (and I know passing functions in general works in haskell). I want to work with the Eq operator in my bar function depending on if the head of my list is smaller, equal or bigger than 0. But the way I did it in my code snippet below I get a compiler error. Sorry if I am missing something obvious here I am pretty new to haskell, your help and explanations are appreciated.
foo :: [x] -> [x]
foo x
| head x < 0 = bar (<) x
| head x == 0 = bar (==) x
| head x > 0 = bar (>) x
bar :: (a -> a -> Bool) -> [x] -> [x]
bar func x = ...
CodePudding user response:
You need to add an Ord
constraint to bar
, since you can't use values of any type with (<)
et al, only those with an Ord
instance. You also need to use a
throughout the type; using x
implies that the type of list passed is independent of the Ord
constraint, which would prevent you from using func
on an element of the argument x
.
bar :: Ord a => (a -> a -> Bool) -> [a] -> [a]
bar func x = ...
Since Eq
is a superclass of Ord
, you don't need to specify Eq
explicitly; an instance of Ord
implies an instance of Eq
.
You need the constraint on foo
as well, since you use <
, ==
, and >
in the guards, not just as arguments to bar
. To emphasize exhaustivity, try using compare
instead of three separate guards
foo :: Ord a => [a] -> [a]
foo x = let f = case compare (head x) 0 of
LT -> (<)
EQ -> (==)
GT -> (>)
in bar f x
(And consider what, if anything, needs to be done about an empty list argument to foo
.)