在GHCI或其他基于Haskell的REPL中,历史管理是如何工作的?由于Haskell是一种纯语言,所以我想它是使用monad实现的,也许是州单。
请注意,我是一个在Haskell的初学者,所以请提供一个详细的解释,而不是仅仅链接到源代码。
发布于 2016-06-19 17:08:37
这是一个简单的例子,说明程序如何保存用户输入的命令的历史记录。它的结构与猜数游戏基本相同,因此,一旦您理解了您应该不难理解这一点:
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 []https://stackoverflow.com/questions/37908718
复制相似问题