0

In which programming languages is a conditional block (if) not syntactically different than a function invocation, including any "else" block of code to be executed in the false condition? In other words, in what languages could one define a function an_alternative_if_function which could then be invoked interchangeably with if?

For example, in Ruby, an if clause could be represented like this:

if(a > 20){
  puts("A is greater than 20.")
}

In that block, at least, if appears similar to any other function. It appears as if this if function is then passed an expression as an argument/parameter and also passed a block of code, as does, in fact, occur in the following functional Ruby code fragment:

def an_alternative_if_function(condition){
  yield if condition
}

an_alternative_if_function(a > 20){
  puts("A is greater than 20.")
}

However, Ruby is not a correct answer to my question, because there is no way for a Ruby function to accept an else clause in the way that if does. The following is incorrect syntax:

an_alternative_if_function(a > 20){
  puts("A is greater than 20.")
} else {
  puts("A is not greater than 20.")
}

Update: My guess about Lisp below is incorrect. See Answers.

On the other hand, I reckon Lisp is one correct answer to my question, because in the following example code, if appears as a function which accepts 3 arguments:

(if (> a 20)
   (write-line "A is greater than 20.") 
   (write-line "A is not greater than 20."))

I'm unconcerned about the implementation details of compilers and interpreters. My only concern is whether if is syntactically distinguishable from other functions to the programmer reading the code.

2
  • I believe this is true of Lisp, but don't quote me ;--) Commented Oct 4, 2023 at 22:12
  • It seems to me that Tcl can do what you say. Commented Oct 5, 2023 at 6:20

1 Answer 1

0

Smalltalk is one example where there is no special syntax for if - there's just an ifTrue: message of a boolean object.

If you consider pure lambda calculus a language, there's no special if there too - you just use Church booleans instead.

Same goes for combinatory systems like SK, with the same Church booleans.

In a Lisp, unless it's some very exotic lazy Lisp, if is a special form and not a function application, for function arguments must be evaluated first in most Lisps.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for correcting me that if requires lazy evaluation of parameters, whereas functions in most programming languages, including Lisp, use strict evaluation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.