Most active commenters
  • antonvs(3)

←back to thread

217 points todsacerdoti | 31 comments | | HN request time: 2.058s | source | bottom
Show context
userbinator ◴[] No.44506358[source]
Somewhat less frequently, I also hear "invoke" or "execute", which is more verbose but also more generic.

Incidentally, I find strange misuses of "call" ("calling a command", "calling a button") one of the more grating phrases used by ESL CS students.

replies(7): >>44506396 #>>44506421 #>>44506500 #>>44506580 #>>44510160 #>>44510878 #>>44511635 #
1. pansa2 ◴[] No.44506396[source]
> 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.”

replies(4): >>44506779 #>>44506910 #>>44506977 #>>44511912 #
2. jamesfinlayson ◴[] No.44506779[source]
I remember someone in university talking about the if function (which ostensibly takes one boolean argument).
replies(6): >>44506886 #>>44506915 #>>44507329 #>>44508128 #>>44509940 #>>44512872 #
3. Findecanor ◴[] No.44506886[source]
There are languages in which `if` is a function.

In in Tcl, `if` is called a "command".

replies(1): >>44509377 #
4. userbinator ◴[] No.44506910[source]
I've heard that too --- the voice in my head automatically read it in the customary thick Indian accent.
5. weinzierl ◴[] No.44506915[source]
Sounds 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.

replies(2): >>44509778 #>>44511669 #
6. pwdisswordfishz ◴[] No.44506977[source]
Eh, "return" is just a very restricted continuation with special syntax… it's a stretch to say you "call" it, but not unjustified.
7. antonvs ◴[] No.44507329[source]
Try 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.

replies(2): >>44509661 #>>44513639 #
8. Delphiza ◴[] No.44508128[source]
In 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)
replies(1): >>44508460 #
9. EForEndeavour ◴[] No.44508460{3}[source]
Excel cell formulas are the most widely used functional programming language in the world.
10. spacechild1 ◴[] No.44509377{3}[source]
Also in Smalltalk and sclang (Supercollider language)
replies(1): >>44510065 #
11. immibis ◴[] No.44509661{3}[source]
I 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.

replies(2): >>44510086 #>>44512726 #
12. kevindamm ◴[] No.44509778{3}[source]
I would include the cond function from lisp, or the generalization from lambda calculus

   λexpr1.λexpr2.λc.((c expr1) expr2)
13. Zambyte ◴[] No.44509940[source]
If 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.
replies(1): >>44510291 #
14. kitd ◴[] No.44510065{4}[source]
Or anything Lispy
15. fn-mote ◴[] No.44510086{4}[source]
A 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/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.

replies(1): >>44512176 #
16. dylan604 ◴[] No.44510291{3}[source]
Huh? if (true) {} takes precisely one argument.
replies(2): >>44510378 #>>44514732 #
17. mjburgess ◴[] No.44510378{4}[source]
That'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)`

replies(1): >>44510682 #
18. devnullbrain ◴[] No.44510682{5}[source]
You 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

    (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

    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://doc.rust-lang.org/reference/expressions/if-expr.html...

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

expression != argument

replies(1): >>44510881 #
19. smilliken ◴[] No.44510881{6}[source]
They aren't talking about C and its descendants in particular, but more generally. For example in Haskell and Scheme there is only an if function and no if statement. And you're welcome to create an if function in any language you like and use it instead of the native syntax. I like to use an if function in PostgreSQL because it's less cumbersome than a case expression.

So in the abstract, if is a ternary function. I think the original comment was reflecting on how "if (true) ... " looks like a function call of one argument but that's obviously wrong.

replies(2): >>44511305 #>>44511773 #
20. devnullbrain ◴[] No.44511305{7}[source]
Arguments are expressions in Haskell. In abstract, it uses expressions.
21. zahlman ◴[] No.44511669{3}[source]
> 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

replies(1): >>44513684 #
22. disconcision ◴[] No.44511773{7}[source]
this is not quite right. haskell and scheme have if expressions, not if statements. that's not the same as if being a function. if is not, and cannot be, a function in scheme, as it does not have scheme function semantics. specifically, it is not strict, as it does not evaluate all its subexpressions before executing. since haskell is non-strict, if can be implemented as a function, and iirc it is
replies(1): >>44512138 #
23. layer8 ◴[] No.44511912[source]
Some people use parentheses for the return value, to make it look like a function call:

    return(value);
24. trealira ◴[] No.44512138{8}[source]
> since haskell is non-strict, if can be implemented as a function, and iirc it is

"If" can be implemented as a function in Haskell, but it's not a function. You can't pass it as a higher-order function and it uses the "then" and "else" keywords, too. But you could implement it as a function if you wanted:

  if' :: Bool -> a -> a
  if' True x _ = x
  if' False _ y = y
Then instead of writing something like this:

  max x y = if x > y then x else y
You'd write this:

  max x y = if' (x > y) x y
But the "then" and "else" remove the need for parentheses around the expressions.
25. ◴[] No.44512176{5}[source]
26. antonvs ◴[] No.44512726{4}[source]
With a function version of `if`, in general the compiler needs to wrap the alternative in closures ("thunks"), as it does with all function arguments unless optimizations make it unnecessary. That's never needed in the syntactic version. That's one significant optimization.

In GHC, `if` desugars to a case statement, and many optimizations flow from that. It's pretty central to the compiler's operation.

> Maybe it was needed in early versions. Or maybe they just didn't know they wouldn't need it yet.

Neither of these are true. My comment above was attempting to explain why `if` isn't implemented as a function. Haskell is a prime example of where it could have been done that way, the authors are fully aware of that, but they didn't because the arguments against doing it are strong. (Unless you're implementing a scripting-language type system where you don't care about optimization.)

27. ahartmetz ◴[] No.44512872[source]
I frequently see people treating if as if it was "taking a comparison", so: if (variable == true) ...
28. igouy ◴[] No.44513639{3}[source]
Isn't practical in Smalltalk either, so the compiler does something special:

    ifFalse: alternativeBlock
        "Answer the value of alternativeBlock. Execution does not actually
         reach here because the expression is compiled in-line."

        ^alternativeBlock value
replies(1): >>44513838 #
29. igouy ◴[] No.44513684{4}[source]
Except

https://news.ycombinator.com/item?id=44513639

30. antonvs ◴[] No.44513838{4}[source]
Oh thanks, I didn't know that. I thought it just relied on the explicit code blocks.

But yeah, this is a pretty critical point for optimizations - any realistic language is likely to optimize this sooner or later.

31. andriamanitra ◴[] No.44514732{4}[source]
Depends on the language! If "if" wasn't a keyword, in Ruby that would be calling a method that takes one positional argument and one block argument, such as `def if(cond, &body) = cond && body.call`. In PureScript that could be a call to a function with signature `if :: Boolean -> Record () -> _`.

But I assume the comment you were replying to was not referring to the conditional syntax from C-like languages, instead referring to a concept of an if "function", like the `ifelse` function in Julia [1] or the `if` form in Lisps (which shares the syntax of a function/macro call but is actually a special form) [2], neither of which would make sense as one argument function.

[1] https://docs.julialang.org/en/v1/base/base/#Base.ifelse

[2] https://www.gnu.org/software/emacs/manual/html_node/elisp/Co...