Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

This value is not a function and cannot be applied - F#

Tags:

f#

// 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?!

like image 551
Luke Xu Avatar asked Sep 06 '25 03:09

Luke Xu


1 Answers

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")
like image 51
vcsjones Avatar answered Sep 09 '25 17:09

vcsjones