gpt4 book ai didi

f# - "let is unfinished. expect an expression"错误。我不知道在哪里

转载 作者:行者123 更新时间:2023-12-01 09:52:21 26 4
gpt4 key购买 nike

open System

let highLowGame () =
let rng = new Random();
let secretNumber = rng.Next() % 100 + 1

let rec highLowGameStep () =
printfn "Guess a number: "
let guessStr = Console.ReadLine()
let guess = Int32.Parse(guessStr)
match guess with
| _ when guess > secretNumber -> printfn "Too high!" highLowGameStep ()
| _ when guess = secretNumber -> printfn "You got it!" ()
| _ when guess < secretNumber -> printfn "Too low!" highLowGameStep ()


[<EntryPoint>]
let main argv =
highLowGame ()
0 // return an integer exit code

我知道有很多这样的问题,而且我知道 F# 中的函数必须有一个返回变量。我的在这里 | _ 当guess = secretNumber -> printfn “你明白了!” () 所以我不明白为什么它一直告诉我我的 block 未完成

此示例直接来自 F# 3.0 书籍。

/stdin(14,13): error FS0010: Unexpected identifier in expression. Expected incomplete structured construct at or before this point or other token.

是完整的错误。

最佳答案

您必须在 let 语句的末尾返回一些内容。否则,您的函数只定义了一些值,但表达式不完整 - 您缺少返回值。 See this MSDN link for more details.

这种情况下,可以在最后加上highLowGameStep()来调用函数并获取其返回值:

open System

let highLowGame () =
let rng = new Random();
let secretNumber = rng.Next() % 100 + 1

let rec highLowGameStep () =
printfn "Guess a number: "
let guessStr = Console.ReadLine()
let guess = Int32.Parse(guessStr)
match guess with
| _ when guess > secretNumber -> printfn "Too high!" highLowGameStep ()
| _ when guess = secretNumber -> printfn "You got it!" ()
| _ when guess < secretNumber -> printfn "Too low!" highLowGameStep ()

highLowGameStep ()


[<EntryPoint>]
let main argv =
highLowGame ()
0 // return an integer exit code

关于f# - "let is unfinished. expect an expression"错误。我不知道在哪里,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35617872/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com