4 ms·
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 argume
by Zambyte 1y ago
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.
- 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
- smilliken 1y agoThey 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.
- devnullbrain 1y agoArguments are expressions in Haskell. In abstract, it uses expressions.
- disconcision 1y agothis 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
- trealira 1y ago> 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.
- Zambyte 1y agoif in Scheme can be, and in some cases is, implemented as a macro though. Which has arguments and can be called like a function.
- andriamanitra 1y agoDepends 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 https://docs.julialang.org/en/v1/base/base/#Base.ifelse [2] https://www.gnu.org/software/emacs/manual/html_node/elisp/Conditionals.html#index-if https://www.gnu.org/software/emacs/manual/html_node/elisp/Co...
- Zambyte 1y agoI count two: true and void. This becomes obvious in languages that have consistent application syntax like Lisp, which would write this as (if true '()) Or if you wanted to capture the exact same semantics (rather than returning a null value to the continuation of the if) (if true (values)) Now it's obvious that if takes two (or three) arguments :)