0

这是到目前为止的完整代码:

module clean
#light
open System
open System.IO
let pause() = Console.ReadLine()
let drive = System.IO.Directory.GetDirectoryRoot(System.IO.Directory.GetCurrentDirectory())
printfn "You're using the %s drive.\n\n" drive

let game1 = "Assassin's Creed"
let game2 = "Crysis"
let game3 = "Mass Effect"

let local1 = "\%APPDATA\%\\Ubisoft\\Assassin's Creed\\Saved Games\\"
let local2 = "\%USERPROFILE\%\\Documents\\My Games\\Crysis\\SaveGames\\"
let local3 = "\%USERPROFILE\%\\Documents\\BioWare\\Mass Effect\\Save\\"

let roam1 = drive + "Saves\\Abraxas\\" + game1 + "\\"
let roam2 = drive + "Saves\\Abraxas\\" + game2 + "\\"
let roam3 = drive + "Saves\\Abraxas\\" + game3 + "\\"




let rec getGame() =
  printfn "Which Game?\n\n   1.%s\n   2.%s\n   3.%s\n\n" game1 game2 game3
  match Int32.TryParse(stdin.ReadLine()) with
  | true,1 -> game1
  | true,2 -> game2
  | true,3 -> game3
  | _ ->
     printfn "You did not enter a valid choice."
     let _ = pause()
     Console.Clear()
     getGame()

let mutable gameprint = getGame()
printf "You have chosen %s\n\n" gameprint

let roaming =
  match gameprint with
  | game1 -> roam1
  | game2 -> roam2
  | game3 -> roam3
  | _ -> "test"

printf "Roaming set to %s\n\n" roaming

let local =
  match gameprint with
  | game1 -> local1
  | game2 -> local2
  | game3 -> local3
  | _ -> "test"

printf "Local set to %s\n\n" local

printf "Check gameprint  %s" gameprint

在设置漫游和本地对象的部分中,它告诉我它永远不会与“game1”以外的任何东西匹配。

我做了'printf'来检查与本地和漫游对象匹配之前和之后......游戏打印在两个printf命令中都正确显示,但与game1以外的任何东西都不匹配......我不确定我犯错的地方。

4

3 回答 3

4

两件事情。

  1. 在 F# 中,绑定可以被遮蔽。特别是,在您的matchgame1game2game3模式中,您实际上是在声明具有这些名称的绑定。因此,第一个模式将始终匹配,并且只会game1在评估右侧之前将您尝试与之匹配的任何值分配给新绑定。

    解决此问题的一种方法是使用[<Literal>]属性声明您的 gameN 绑定(但请注意,它们也必须以大写字母开头才能用作常量):

    [<Literal>] let Game1 = "Assassin's Creed"

    现在您可以Game1在模式匹配中使用它,它会按您的预期工作。

  2. 您可能已经意识到这一点,但实际上您并没有在gameprint任何地方更新绑定,因此它将在整个程序中设置为相同的值,并且它是可变的毫无意义。

于 2010-09-16T03:53:46.747 回答
3

有关说明,请参阅F# 与两个值匹配

在与一些非文字值进行比较时,我只使用 if-then-else

if gameprint = game1 then ...
elif gameprint = game2 then ...
...
于 2010-09-16T03:40:28.177 回答
1

也许更像这样的东西可以让你使它更具可扩展性(如果你在运行时填充游戏列表)......对不起,代码有点匆忙我正在努力准备工作:

open System 
type Game = {Title:string; local:string; roam:string}

let game1 = {
    Title= "Assassin's Creed"; 
    local = "\%APPDATA\%\\Ubisoft\\Assassin's Creed\\Saved Games\\"; 
    roam = "Saves\\Abraxas\\\Assassin's Creed\\"
}
let game2 = {
    Title= "Crysis"; 
    local = "\%USERPROFILE\%\\Documents\\My Games\\Crysis\\SaveGames\\"; 
    roam = "Saves\\Abraxas\\\Crysis\\"
}
let games = [game1; game2]
let printGamelListItem i g = printfn "%i: %s" i g.Title

let printChoice() = 
    printfn "Which Game?\n"
    games |> List.fold (fun acc g -> 
                            printGamelListItem acc g 
                            acc+1) 1
    |> ignore

let rec getGame() = 
    printChoice()
    match Int32.TryParse(Console.ReadLine()) with
    |true, x  when x <= games.Length -> games.[x-1]
    | _ ->
        printfn "You did not enter a valid choice."
        let _ = Console.ReadLine()
        Console.Clear()
        getGame()

let selection = getGame()
printfn "Roaming set to: %s" (selection.roam)
printfn "Local set to: %s" (selection.local)
于 2010-09-16T06:50:27.567 回答