Reputation: 133
I wrote the following function
foldList :: (Double -> Double -> Double) -> [Double] -> Double
foldList op (x:t)
| t == [] = x
| otherwise = (op) x (foldList op t)
and it worked perfectly fine. But when I changed the last line to
| otherwise = x op (foldList op t)
it didn't compile anymore. I am still rather new to Haskell but I thought when dealing with operators
a op b
is equivalent to
(op) a b
Do I have to treat op
as just a normal function? And if so, in what cases is it regarded an operator and why not here?
Upvotes: 1
Views: 95
Reputation: 225281
Operators are functions with symbol names. They’re infix by default, and you can use them like other functions by wrapping them in parentheses.
a + b (+) a b
Functions with identifier names, like your op
, can be used as infix by wrapping them with backticks.
op a b a `op` b
See also https://wiki.haskell.org/Infix_operator
Upvotes: 6