nazila
nazila

Reputation: 15

if statement checking multiple conditions in SML

I am new to SML and I have written a program that takes 3 integer numbers (n,z,k) and wants to compare them with a value given in the program, if the statement is correct it will return true, else false. conditions and values should be equal simultaneously, I mean program will return true if all these numbers are equal to the value given in the program, so my if statement should check 3conditions at the same time. my problem is that I don't know how I can write all these 3conditions in one if clause, because SML don't recognize for example & or ^ as and! for example i want to check whether if(n==8 && k==5 && z==9). what should I use instead of & here is the code:

fun EvalR (n: int , k: int , z:int) = 
if (n=8 ???) then true
else false;

Upvotes: 1

Views: 1505

Answers (2)

sshine
sshine

Reputation: 16105

Since Ashkan Parsa referred to the CS317 SML Style Guide, I thought I would point out what you might derive from it.

  1. Let function names start with a lower case, e.g. evalR.

  2. Don't write if ... then true else false; simply write ....

  3. Some disagree; type annotations certainly are helpful, but so is type inference.

  4. As nazila says, the and operator in Standard ML is called andalso.

So,

fun evalR (n, k, z) =
    n = 42 andalso k = 43 andalso z = 0

It might seem comfusing that the function body contains =s at the same time as the function being defined with a = to separate the function arguments from the function body. Just think of the latter =s as value operators and the first = as a part of declaring things (like types, values, functions, etc.)

Upvotes: 1

nazila
nazila

Reputation: 15

I found it. we can use andalso in SML.

Upvotes: 0

Related Questions