8

I am new to SML. How do I use the AND operator inside IF statements? Here is my code:

val y = 1;
val x = 2;
if (x = 1 AND y = 2) then print ("YES ") else print("NO ");

My error is: stdIn:66.9-67.3 Error: unbound variable or constructor: AND stdIn:66.3-67.9 Error: operator is not a function [literal] operator: int in expression: 1 stdIn:66.3-67.9 Error: operator and operand don't agree [literal] operator domain: bool * bool operand: bool * int in expression: x = (1 ) y = 2

Thank you

Alexander Tilkin
  • 149
  • 1
  • 2
  • 13

4 Answers4

13

There is no AND operator in SML (unless you define one yourself). There is an and keyword, but you can't use it inside if statements (or generally as a part of any expression) because it's not an operator. It's used in combination with fun to define mutually recursive functions.

You're probably looking for the andalso operator, which takes two boolean operands and returns true if and only if both operands are true.

Andreas Rossberg
  • 34,518
  • 3
  • 61
  • 72
sepp2k
  • 363,768
  • 54
  • 674
  • 675
1

May I disagree with Vishal's comment?

*-true andalso true ;     
val it = false : bool
-true andalso false ;  
val it = false : bool*

I think (and so does the REPL) that

true andalso true ;

evaluates to true, not false

birrellwalsh
  • 143
  • 8
-1

here is an example which will clear the usage of andalso

-true andalso true ;
val it = false : bool
- true andalso false ; 
val it = false : bool
vishal
  • 855
  • 1
  • 8
  • 16
-1

one and one in digital gates is always 1; therefore in the above case true andalso true "must necessarily evaluate to true , not false; am'i correct ? also 1 and 0 can't never be 1;

1 or 0 can be 1;