历史管理在 GHCI 或其他基于 Haskell 的 REPL 中如何工作?由于 Haskell 是一种纯语言,我猜它是使用 monad 实现的,也许是状态单子 http://learnyouahaskell.com/for-a-few-monads-more#state.
请注意,我是 Haskell 的初学者,因此请提供详细的解释,而不仅仅是链接到源代码。
这是程序如何保存用户输入的命令历史记录的简化示例。它基本上与猜数字游戏具有相同的结构,因此一旦您了解了这一点,您应该不会有任何困难:
import Control.Monad.State
import Control.Monad
shell :: StateT [String] IO ()
shell = forever $ do
lift $ putStr "$ "
cmd <- lift getLine
if cmd == "history"
then do hist <- get
lift $ forM_ hist $ putStrLn
else modify (++ [cmd])
main = do putStrLn "Welcome to the history shell."
putStrLn "Type 'history' to see your command history."
execStateT shell []
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)