2 ms·
I don't know ML, but I really do wish Python had this feature. The only problem I see is that this makes it hard to refer to a function without calling it (see
by marvy 11y ago
I don't know ML, but I really do wish Python had this feature. The only problem I see is that this makes it hard to refer to a function without calling it (see below). How does ML solve this?
def g(): return 5
assert g() == 5
assert g != 5
Without parenthesis, how can the language distinguish the last two lines? How does ML do it? Would the same trick work in Python? I know Ruby has optional parenthesis, but functions aren't quite as first class there as they are in Python, which is why the have Proc and whatnot.
- cheepin 11y agoYour example, g actually takes the unit tuple, and returns 5. In ML, in general, you have to disambiguate with parentheses sometimes, but the compiler is very strict about types, so it's usually pretty easy to tell where they need to be. Look here: http://try.ocamlpro.com/ http://try.ocamlpro.com/ Try this code: let g() = 5 g() == 5 g != 5 First line defines the function. Second line should evaluate to true. Third line doesn't type check because a function is not the same type as a literal.
- eru 11y agoExactly. My favourite ML-style language is Haskell, and there we almost never want to call a function with the unit-tuple as an argument. Haskell does differentiate between IO-actions and functions. Here is an example: main = do let printFive = print 5 print 1 printFive print 6 printFive Will print 1 5 6 5 The type systems helps enormously keeping things straight here, but the basic concept would work in a non-statically typed language, too.
- marvy 11y ago> the basic concept would work in a non-statically typed language, too How? How do I compare. say, two functions for equality, without comparing the return value instead?
- eru 11y agoOh, I should have been more explicit: The decoupling of function calling and IO actions can work in non-statically typed languages, too. Your question concerns a different topic. To give something like an answer: that problem can only occur with `functions' of zero arguments. Haskell sidesteps the issue by requiring all functions to have exactly one argument.
- marvy 11y agoOkay, that's cheating. Or rather, that's fine for them, but is no help in porting this idea to Python, which definitely has functions of zero arguments, and they aren't even that rare. (Although comparing functions for equality is rare, but it should at least be possible.)