// Learn more about F# at http://fsharp.org
// See the 'F# Tutorial' project for more help.
open System
let highLowGame () =
let rng = new Random()
let secretNumber = rng.Next() % 100 + 1
let rec highLowGameStep () =
printfn "Guess the secret number: "
let guessStr = Console.ReadLine()
let guess = Int32.Parse(guessStr)
match guess with
| _ when guess > secretNumber
-> (printfn "The secret number is lower")
highLowGameStep ()
| _ when guess = secretNumber
-> (printfn "You've guessed correctly!")
()
| _ when guess < secretNumber
-> (printfn "The secret number is higher")
highLowGameStep ()
highLowGameStep ()
[<EntryPoint>]
let main argv =
printfn "%A" argv
0 // return an integer exit code
I've checked this code like a thousand times and I get a warning saying guess is incomplete on pattern matching
which is definitely impossible... and I also get an error saying the value is not a function and cannot be applied.
I copied this straight out of a book so I don't get how this is not compiling?!
The white space on the pattern match is wrong. It should be something like this:
match guess with
| _ when guess > secretNumber ->
(printfn "The secret number is lower")
highLowGameStep ()
| _ when guess = secretNumber ->
(printfn "You've guessed correctly!")
()
| _ when guess < secretNumber ->
(printfn "The secret number is higher")
highLowGameStep ()
That fixes the compilation error. As for the warning, the compiler is making a best guess, but it isn't able to determine that all cases are actually covered. If you want to fix the warning, you can add another case:
| _ -> invalidOp("Invalid input")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With