Most active commenters

    ←back to thread

    166 points todsacerdoti | 19 comments | | HN request time: 1.685s | 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 #
    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 #
    1. jamesfinlayson ◴[] No.44506779[source]
    I remember someone in university talking about the if function (which ostensibly takes one boolean argument).
    replies(5): >>44506886 #>>44506915 #>>44507329 #>>44508128 #>>44509940 #
    2. Findecanor ◴[] No.44506886[source]
    There are languages in which `if` is a function.

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

    replies(1): >>44509377 #
    3. 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 #
    4. 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(1): >>44509661 #
    5. 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 #
    6. EForEndeavour ◴[] No.44508460[source]
    Excel cell formulas are the most widely used functional programming language in the world.
    7. spacechild1 ◴[] No.44509377[source]
    Also in Smalltalk and sclang (Supercollider language)
    replies(1): >>44510065 #
    8. immibis ◴[] No.44509661[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(1): >>44510086 #
    9. kevindamm ◴[] No.44509778[source]
    I would include the cond function from lisp, or the generalization from lambda calculus

       λexpr1.λexpr2.λc.((c expr1) expr2)
    10. 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 #
    11. kitd ◴[] No.44510065{3}[source]
    Or anything Lispy
    12. fn-mote ◴[] No.44510086{3}[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.

    13. dylan604 ◴[] No.44510291[source]
    Huh? if (true) {} takes precisely one argument.
    replies(1): >>44510378 #
    14. mjburgess ◴[] No.44510378{3}[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 #
    15. devnullbrain ◴[] No.44510682{4}[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 #
    16. smilliken ◴[] No.44510881{5}[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 #
    17. devnullbrain ◴[] No.44511305{6}[source]
    Arguments are expressions in Haskell. In abstract, it uses expressions.
    18. zahlman ◴[] No.44511669[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

    19. disconcision ◴[] No.44511773{6}[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