Seb
Seb

Reputation: 69

F#: how to obtain a char type expression with an int type input

I'm trying to write a simple code that has int type as input variables and gives me a char type output variable depending of the values of the input variables:

    let control (a : int) (b : int) (c : int) : char = match (a,b,c) with (1,1,1) ->'r'

    control 1 1 1

it works but only for the combination 1 1 1

How can I make to have an output 't' (for example) if a = b = c and 'n' for other combinations?

thank you so much

Upvotes: 2

Views: 78

Answers (1)

ildjarn
ildjarn

Reputation: 62975

let control a b c =
    match (a, b, c) with
      | (1, 1, 1)             -> 'r'
      | _ when a = b && b = c -> 't'  // or  when (a, b) = (b, c)
      | _                     -> 'n'

Or:

let control a b c =
    if (a, b, c) = (1, 1, 1) then 'r'
    elif a = b && b = c then 't'      // or  elif (a, b) = (b, c) then
    else 'n'

Upvotes: 4

Related Questions