我正在阅读F#中的 Lisp Land of Lisp一书(是的,很奇怪,我知道)。对于他们的第一个示例文本冒险,他们使用了全局变量突变,我想避免它。我的 monad-fu 很弱,所以现在我正在做这样的丑陋状态传递:
let pickUp player thing (objects: Map<Location, Thing list>) =
let objs = objects.[player.Location]
let attempt = objs |> List.partition (fun o -> o.Name = thing)
match attempt with
| [], _ -> "You cannot get that.", player, objs
| thing :: _, things ->
let player' = { player with Objects = thing :: player.Objects }
let msg = sprintf "You are now carrying %s %s" thing.Article thing.Name
msg, player', things
let player = { Location = Room; Objects = [] }
let objects =
[Room, [{ Name = "whiskey"; Article = "some" }; { Name = "bucket"; Article = "a" }];
Garden, [{ Name = "chain"; Article = "a length of" }]]
|> Map.ofList
let msg, p', o' = pickUp player "bucket" objects
// etc.
如何分解显式状态以使其更漂亮?(假设我可以访问 State monad 类型,如果它有帮助的话;我知道 F# 中有它的示例代码。)