gpt4 book ai didi

f# - 我如何在 F# 中编写经典的高/低游戏?

转载 作者:行者123 更新时间:2023-12-04 09:37:51 24 4
gpt4 key购买 nike

我正在阅读函数式语言,我想知道如何在 中实现“尝试”纯 功能语言。所以我决定尝试在 F#

但我无法获得一半的基础知识。我不知道如何使用随机数,如何使用返回/继续(起初我以为我在做一个多语句,如果错了,但似乎我做对了),我不知道如何打印一个数字在 F# 中,所以我以 C# 的方式进行。

更难的问题是 tryparse 中的输出参数,我仍然不确定我将如何实现 tries不使用可变变量。也许你们中的一些人可以告诉我如何正确实现这一点

我上周必须做的 C# 代码

using System;

namespace CS_Test
{
class Program
{
static void Main(string[] args)
{
var tries = 0;
var answer = new Random().Next(1, 100);
Console.WriteLine("Guess the number between 1 and 100");
while (true)
{
var v = Console.ReadLine();
if (v == "q")
{
Console.WriteLine("you have quit");
return;
}
int n;
var b = Int32.TryParse(v, out n);
if (b == false)
{
Console.WriteLine("This is not a number");
continue;
}
tries++;
if (n == answer)
{
Console.WriteLine("Correct! You win!");
break;
}
else if (n < answer)
Console.WriteLine("Guess higher");
else if (n > answer)
Console.WriteLine("Guess lower");
}
Console.WriteLine("You guess {0} times", tries);
Console.WriteLine("Press enter to exist");
Console.ReadLine();
}
}
}

非常 splinter 和错误的 F# 代码
open System;

let main() =
let tries = 0;
let answer = (new Random()).Next 1, 100
printfn "Guess the number between 1 and 100"
let dummyWhileTrue() =
let v = Console.ReadLine()
if v = "q" then
printfn ("you have quit")
//return
printfn "blah"
//let b = Int32.TryParse(v, out n)
let b = true;
let n = 3
if b = false then
printfn ("This is not a number")
//continue;
//tries++
(*
if n = answer then
printfn ("Correct! You win!")
//break;
elif n < answer then
printfn ("Guess higher")
elif n>answer then
printfn ("Guess lower")
*)
dummyWhileTrue()
(Console.WriteLine("You guess {0} times", tries))
printfn ("Press enter to exist")
Console.ReadLine()
main()

最佳答案

欢迎来到 F#!

这是一个工作程序;解释如下。

open System

let main() =
let answer = (new Random()).Next(1, 100)
printfn "Guess the number between 1 and 100"
let rec dummyWhileTrue(tries) =
let v = Console.ReadLine()
if v = "q" then
printfn "you have quit"
0
else
printfn "blah"
let mutable n = 0
let b = Int32.TryParse(v, &n)
if b = false then
printfn "This is not a number"
dummyWhileTrue(tries)
elif n = answer then
printfn "Correct! You win!"
tries
elif n < answer then
printfn "Guess higher"
dummyWhileTrue(tries+1)
else // n>answer
printfn "Guess lower"
dummyWhileTrue(tries+1)
let tries = dummyWhileTrue(1)
printfn "You guess %d times" tries
printfn "Press enter to exit"
Console.ReadLine() |> ignore
main()

很多事情...

如果您使用多个参数调用方法(例如 Random.Next ),请在参数周围使用括号( .Next(1,100) )。

您似乎正在处理递归函数( dummyWhileTrue )而不是 while 循环;一个while循环也可以,但我保持你的方式。注意没有 breakcontinue在 F# 中,因此您必须使用 if 更有条理里面的东西。

我改变了你的 Console.WriteLineprintfn炫耀如何用参数调用它。

我展示了调用 TryParse的方法这最像 C#。首先声明你的变量(让它可变,因为 TryParse 将写入该位置),然后使用 &n作为参数(在这种情况下, &n 类似于 C# 中的 ref nout n)。或者,在 F# 中,您可以这样做:
let b, n = Int32.TryParse(v)

其中 F# 允许您省略尾随参数,而是在元组末尾返回它们的值;这只是一种语法便利。
Console.ReadLine返回一个字符串,在程序结束时您不关心它,因此将其通过管道传送到 ignore函数丢弃该值(并摆脱有关未使用的字符串值的警告)。

关于f# - 我如何在 F# 中编写经典的高/低游戏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4141976/

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