我想跟踪一系列不可变值中的“当前”值。在不为每个新值引入新引用的情况下,在 Haskell 中做到这一点的最佳方法是什么?这是一个例子:
data Person = Person {name, level, topic :: String }
deriving(Show)
dierk :: Person
dierk = Person "Dierk" "confident" "Java"
works :: Person -> String
works person = name person ++ " is " ++ level person ++ " in " ++ topic person
main _ = do
putStrLn $ works dierk
-- do more with "current" topic
putStrLn $ works dierk {level= "proficient", topic="Groovy"}
-- do more with "current" topic
putStrLn $ works dierk {level= "dabbling", topic="Haskell"}
-- do more with "current" topic