7 ms·
> strange misuses of "call" My favourite (least favourite?) is using “call” with “return”. On more than one occasion I’ve heard: “When we call the return keyw
by pansa2 1y ago
> strange misuses of "call"
My favourite (least favourite?) is using “call” with “return”. On more than one occasion I’ve heard:
“When we call the return keyword, the function ends.”
- jamesfinlayson 1y agoI remember someone in university talking about the if function (which ostensibly takes one boolean argument).
- Findecanor 1y agoThere are languages in which `if` is a function. In in Tcl, `if` is called a "command".
- spacechild1 1y agoAlso in Smalltalk and sclang (Supercollider language)
- kitd 1y agoOr anything Lispy
- jamesfinlayson 1y agoYes I stand corrected - we were using C so definitely not a function there.
- weinzierl 1y agoSounds like something Prof. John Ousterhout would say:-; The place where this was literally accurate would be Tcl. I don't know enough Smalltalk to be sure but I think to remember it has a similar approach of everything is an object and I wouldn't be surprised if they'd coerced control flow somehow into this framework. Also Forth comes to mind, but that would probably be a stretch.
- kevindamm 1y agoI would include the cond function from lisp, or the generalization from lambda calculus λexpr1.λexpr2.λc.((c expr1) expr2)
- zahlman 1y ago> I don't know enough Smalltalk to be sure but I think to remember it has a similar approach of everything is an object and I wouldn't be surprised if they'd coerced control flow somehow into this framework. It does. It's been discussed on HN before, even: https://news.ycombinator.com/item?id=13857174 https://news.ycombinator.com/item?id=13857174
- igouy 1y agoExcept https://news.ycombinator.com/item?id=44513639 https://news.ycombinator.com/item?id=44513639
- antonvs 1y agoTry implementing that in most languages and you'll run into problems. In an imperative programming language with eager evaluation, i.e. where arguments are evaluated before applying the function, implementing `if` as a function will evaluate both the "then" and "else" alternatives, which will have undesirable behavior if the alternatives can have side effects. In a pure but still eager functional language this can work better, if it's not possible for the alternatives to have side effects. But it's still inefficient, because you're evaluating expressions whose result will be discarded, which is just wasted computation. In a lazy functional language, you can have a viable `if` function, because it will only evaluate the argument that's needed. But even in the lazy functional language Haskell, `if` is implemented as built-in syntax, for usability reasons - if the compiler understands what `if` means as opposed to treating it as an ordinary function, it can optimize better, produce better messages, etc. In a language with the right kind of macros, you can define `if` as a macro. Typically in that case, its arguments might be wrapped in lambdas, by the macro, to allow them to be evaluated only as needed. But Scheme and Lisp, which have the right kind of macros, don't define `if` as a macro for similar reasons to Haskell. One language in which `if` is a function is the pure lambda calculus, but no-one writes real code in that. The only "major" language I can think of in which `if` is actually a function (well, a couple of methods) is Smalltalk, and in that case it works because the arguments to it are code blocks, i.e. essentially lambdas. tl;dr: `if` as a function isn't practical in most languages.
- immibis 1y agoI don't think Haskell needs 'if' to be a construct for compiler optimization reasons; it could be implemented easily enough with pattern matching: if' :: Bool -> a -> a -> a if' True x _ = x if' False _ y = y The compiler could substitute this if it knew the first argument was a constant. Maybe it was needed in early versions. Or maybe they just didn't know they wouldn't need it yet. The early versions of Haskell had pretty terrible I/O, too.
- fn-mote 1y agoA short search lead to this SE post [1], which doesn't answer the "why" but says "if" is just syntactic sugar that turns into `ifThenElse`... [1]: https://softwareengineering.stackexchange.com/questions/195708/why-does-haskell-have-built-in-if-then-else-instead-of-defining-it-as-a-simple https://softwareengineering.stackexchange.com/questions/1957... The post claims that this is done in such a basic way that if you have managed to rebind `ifThenElse`, your rebound function gets called. I didn't confirm this, but I believed it.
- Delphiza 1y agoIn Excel formulas everything is a function. IF, AND, OR, NOT are all functions. It is awkward and goes against what software devs are familiar with, but there are probably more people familiar with the Excel IF function than any other forms. Here is an example taken from the docs... =IF(AND(A3>B2,A3<C2),TRUE,FALSE)
- EForEndeavour 1y agoExcel cell formulas are the most widely used functional programming language in the world.
- jamesfinlayson 1y agoYes I stand corrected - we were using C so definitely not a function there.
- Zambyte 1y agoIf takes two or three arguments, but never one. The condition is the one made syntactically obvious in most languages, the consequent is another required argument, and the alternative is optional.
- dylan604 1y agoHuh? if (true) {} takes precisely one argument.
- mjburgess 1y agoThat's an application of `if` with one of the arguments empty. The semantics of `if` requrie at least, `if(cond, clause)`, though more generally, `if(cond, clause, else-clause)`
- devnullbrain 1y agoYou and Zambyte are both doing the same thing the top level comment is complaining about. e.g. in C: https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf (6.8.5.1) selection-statement: if ( expression ) secondary-block if ( expression ) secondary-block else secondary-block in C++: https://eel.is/c++draft/gram.stmt https://eel.is/c++draft/gram.stmt selection-statement: if constexpropt ( init-statementopt condition ) statement if constexpropt ( init-statementopt condition ) statement else statement if !opt consteval compound-statement if !opt consteval compound-statement else statement where condition: expression attribute-specifier-seqopt decl-specifier-seq declarator brace-or-equal-initializer structured-binding-declaration initializer More examples: https://docs.python.org/3/reference/grammar.html https://docs.python.org/3/reference/grammar.html https://doc.rust-lang.org/reference/expressions/if-expr.html#grammar-IfExpression https://doc.rust-lang.org/reference/expressions/if-expr.html... https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... expression != argument
- 1y ago
- ahartmetz 1y agoI frequently see people treating if as if it was "taking a comparison", so: if (variable == true) ...
- lmm 1y agoif should be a function, though sadly many languages aren't good enough to express it and have to make it a builtin.
- userbinator 1y agoI've heard that too --- the voice in my head automatically read it in the customary thick Indian accent.
- pwdisswordfishz 1y agoEh, "return" is just a very restricted continuation with special syntax… it's a stretch to say you "call" it, but not unjustified.
- layer8 1y agoSome people use parentheses for the return value, to make it look like a function call: return(value);