Alagappan Ramu
Alagappan Ramu

Reputation: 2358

Why doesn't SML allow if-then without else?

In Standard ML, what was the reasoning behind having if-then-else as a single expression and not allowing only if-then without else clause?

Is it possible to write conditional statements in SML without the else clause?

Upvotes: 3

Views: 3846

Answers (4)

Hibou57
Hibou57

Reputation: 7170

Because otherwise, what would be the value of the expression if the if branch does not match? To not need the else branch would require a default value can be inferred. The only thing I see which could make sense, is to raise an exception. Could have been an option for the design of SML, but this was not and would not have been a lot relevant any way.

Whenever you feel there is no valid expression value on else, then just say something like this:

val x = 
   if condition then expression 
   else raise Domain;

Upvotes: 0

user2864740
user2864740

Reputation: 61865

Standard ML programs are expressions, not statements.

Standard ML is a functional programming language with some impure features. Programs written in Standard ML consist of expressions to be evaluated, as opposed to statements or commands [as found in C-like languages] ..

As such, because if-then-else is an expression, it must evaluate to a value. If the else was not required then the expression would effectively "have no value" if the condition failed - but by definition of an expression, it must have a value. Requiring an explicit else ensures that the expression will evaluate to value in both cases1.

Furthermore, the type from the then and else expressions must be unified - this will be the type of the entire if-then-else construct.

That is, if-then-else in SML is like the ternary (?:) operator in C-like languages, which also shares this "restriction". It is not equivalent if-statements whose branches are evaluated for side effects only.


1 Not all functional-like languages require an explicit then expression and some will default to a particular value. However, this is just how it works in SML which makes sense because there need not be a "default value" for any particular type and the resulting types must be unified.

Upvotes: 10

ruakh
ruakh

Reputation: 183211

This isn't specific to Standard ML; many or most languages with if-then-else expressions require an else-expression. For example, in C-like languages (C, C++, C#, Java, D, Perl, JavaScript, PHP, etc.), the expression takes the form cond ? expr_if_true : expr_if_false; in Visual Basic the Iif function requires both an expression-if-true and an expression-if-false; and so on. (There are some languages, such as the Excel formula language, that do not require both values, and substitute a default for the else-expression, but SML is hardly exceptional in not doing so.)

Is it possible to write conditional statements in SML without the else clause?

SML doesn't have any concept of "statements", let alone "conditional statements". It has declarations, but it doesn't make sense to declare something only conditionally (since type information is all determined at compile-time, whereas conditions of course can't be evaluated until run-time).

If you want to take a certain action when a condition is true, and take no action when the condition is false, you just need to use a conditional expression where only the then-expression has a side effect. For example:

val _ = if i > 30 then print "i is too big!" else ()

(where print "Yay!" and () are both expressions of type unit).

Upvotes: 4

DMP
DMP

Reputation: 1043

I understand what you are saying, but if the "if" statement of your function returns false then the program doesn't know what to do. You probably just want the function to keep going if the expression is false....right?

If you want that to happen then you have make your "else" do something that just passes on to the rest of the function.

I actually don't know much about SML so i couldn't tell you how to do that

Upvotes: 0

Related Questions