// 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
我已经检查了这个代码一千次了,我收到警告说g uess is incomplete on pattern matching
这绝对是不可能的......而且我也收到错误说value is not a function and cannot be applied.
我直接从书中复制了这些内容,所以我不知道这是怎么编译的?!
答案 0 :(得分:6)
模式匹配上的空白区域错误。它应该是这样的:
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 ()
修复了编译错误。至于警告,编译器正在做出最佳猜测,但它无法确定所有案例都已实际覆盖。如果要修复警告,可以添加其他案例:
| _ -> invalidOp("Invalid input")
答案 1 :(得分:3)
如果在printfn
语句后添加分号,它将起作用。我相信它没有编译的原因是因为你试图从你的匹配中返回printfn
和另一个函数(highLowGameStep)。