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.
Multiple Conditionals in Poly ML
1.2k views Asked by user1775390 At
2
There are 2 answers
0
On
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 *)
Isn't
what you want?