If, for example, I wanted to define a function that returned true if a=b and b=c, and false if neither one of those equalities were true in Poly ML, how would I write it? I'm not sure how to do more than one conditional simultaneously.
Asked
Active
Viewed 1,214 times
1
-
1What do you want to return if only one of the equalities is true? – sepp2k Oct 25 '12 at 20:15
-
Just "false"? Although, I asked around, and it seems to be that andalso is the thing to use. – user1775390 Oct 25 '12 at 20:16
2 Answers
1
I believe this does what you need:
fun f (a, b, c) =
if a = b andalso b = c
then true
else
if a <> b andalso b <> c
then false
else ... (* you haven't specified this case *)
The main points here are:
- You can nest conditionals, i.e. have one
if
expression inside another'sthen
orelse
case - The operator
andalso
is the boolean conjunction, meaningx andalso y
istrue
if and only ifx
evaluates totrue
andy
evaluates totrue
.
You can put this more concisely using a case
expression:
fun f (a, b, c) =
case (a = b, b = c) of
(true, true) => true
| (false, false) => false
| _ => (* you haven't specified this case *)

waldrumpus
- 2,540
- 18
- 44