Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

OCaml definitely has a lot of built-in operators, but Haskell makes it absurdly easy to define your own operators as well, with built-in syntax for using binary operators as prefix functions and vice-versa. I'm not sure there's a definitive answer, but that's probably in large part because the idea of "operator stew" is pretty subjective.


Ocaml makes it super easy to define your own operators as well. I was debugging a performance issue in an Ocaml codebase when I was very junior and new to the language, and proudly reported my finding that we shouldn't be using the (**) power operator in some algorithm but just multiplying and surely this was the cause of the problem. Turned out somebody had defined (**) as 64-bit multiply :)

That said I don't know Haskell to compare!


> Turned out somebody had defined (*) as 64-bit multiply :)

Well I don't think you should have been the one embarrassed in that scenario.


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.


It’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!




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: