mercredi 20 avril 2016

OCaml Function 'guess' without using if/else

I need to make a function in OCaml, which generates a number from 0 to 100, using Random. Then, in the loop, program asks to give a number from that section, compares those 2 numbers and returns statement 'my number is higher' or 'my number is lower', after a correct guess it returns a communicate 'Congratulations! You guessed!'. I've already done it with if/else, but now I have to do it without them, and I have no idea how to do it.

My code:

let guess() =
print_string "Wylosowano z przedzialu [0-100]. ";
let rand = Random.int 101 and number = ref (-1)
in while !number <> rand do
    begin
        print_string "Put a number from [0-100]: ";
        number := read_int();
        if !number < rand then print_string("my number is higher")
        else if !number > rand then print_string("my number is lower")
        else print_endline("Congratulations! You guessed!")
    end
done




Aucun commentaire:

Enregistrer un commentaire