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?
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.