這是迄今爲止全碼:F#匹配可變對象(字符串)
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之外的任何其他內容都匹配...... I我不確定我犯了什麼錯誤。
這應該仍然堅持乾的原則。我不會在任何地方重複任何字符串聲明(我沒有將gamen嵌入到localn中,因爲它不僅僅是gamen,即localn中的更改,不同的遊戲很少會有類似的結構)。 DRY原則的一部分:「邏輯上相關的元素全部可預測和統一地變化」。如果要改變Localn,它永遠不會改變。 – Abraxas000 2010-09-17 13:41:16