Multiple Conditionals in Poly ML

1.2k views Asked by At

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.

2

There are 2 answers

0
Andreas Rossberg On

Isn't

a = b andalso b = c

what you want?

0
waldrumpus 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's then or else case
  • The operator andalso is the boolean conjunction, meaning x andalso y is true if and only if x evaluates to true and y evaluates to true.

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 *)