3 ms·
I don't remember exactly how it works in OCaml (it might be similar!) but in Haskell, I think functions beginning with certain characters are automatically oper
by saghm 5y ago
I don't remember exactly how it works in OCaml (it might be similar!) but in Haskell, I think functions beginning with certain characters are automatically operators, but you can use them prefix by wrapping them in parens, e.g. `2 + 2` is the same as `(+) 2 2`. Similarly, a two-parameter function can be used infix by wrapping in backticks. Off the top of my head, I think I remember getting warnings from the linter about saying something like "elem some_list some_elem" to check if an element existed in the list instead of the preferred "some_list `elem` some_elem". The idea that functions are operators are interchangeable and have first-class syntax for swapping between them is a lot of fun, but also one of those things where I feel like I'm kinda glad it's not mainstream due to the shenanigans that I might have to deal with in production code.
- mcintyre1994 5y agoIt’s been a while but it’s definitely similar in Ocaml, you define a function with a name that consists purely of the set of allowed symbols and it’s useable as an operator. The thing I found really fiddly/tricky is that the first character is used to define whether it’s prefix or infix, and its associativity. Which like you said, it’s very clever and concise and powerful, but can be a bit of a nightmare if you have these operators defined all over the place and have to piece together how they interact. I don’t think Ocaml had that backtick infix syntax for non-symbol function names though!