So I wanted to understand the functions foldl and foldr in Hoogle
they say that these functions apply to binary operators. But I don't get what these binary operators are. I know that (+) and (-) are binary operators but what defines them ?
-
1In Haskell a binary function is a function which doesn't deal with functors, aplicative functors, monadic values etc as arguments or returned values. In another words just simple functions are binary functions.Redu– Redu2017-07-18 19:58:35 +00:00Commented Jul 18, 2017 at 19:58
-
@Redu That's not what a binary function is. I'm not sure I know of a term for what you're describing, actually ("first-order function" almost comes close "in spirit," but they can involve functors, etc).David Young– David Young2017-07-18 23:37:33 +00:00Commented Jul 18, 2017 at 23:37
Add a comment
|
1 Answer
Usually mathematicians define a binary operator is a function that takes two arguments. In some papers it is a requirement that the arguments and the result are defined over the same set.
The fact that the function ? takes two arguments results in the fact that mathematicians frequently use infix notation (like x ? y). For instance (+), (*), etc. are clearly binary operators.
8 Comments
IPiiro
Oh so that binary stands for the operator is taking two elements.. well that should be obvious to me. Thanks
sepp2k
I don't think the term usually implies that both operands are from the same set. Like, I'd imagine most people consider division to be a binary operator even though it's type is
R * R \ {0} -> R, not R * R -> R. And in the specific context of the fold functions, "binary operator" definitely does not imply that the operand types must be the same, as that is not the case.IPiiro
Yes you are right but the word binary is kind of a hint for that.
GarethR
@sepp2k surely the division operator is partial with type
R * R -> R?amalloy
(:) is an obvious example of a binary operator whose two arguments are not of the same type. |