用ghci记录历史 [英] Keeping track of history in ghci
本文介绍了用ghci记录历史的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
$ b $ b
解决方案
这是程序如何保存用户输入的命令历史的简单示例。它基本上和数字猜测游戏具有相同的结构,所以一旦你明白你应该没有理解这一点:
import Control.Monad.State
import Control.Monad
$ b $ shell :: StateT [String] IO()
shell = forever $ do
lift $ putStr$
cmd< - lift getLine
if cmd ==history
然后执行hist< - get
lift $ forM_ hist $ putStrLn
else modify(+ + [cmd])
main = do putStrLn欢迎使用history shell。
putStrLn输入'history'查看您的命令历史记录。
execStateT shell []
How does history management work in GHCI or other Haskell-based REPLs? Since Haskell is a pure language, I guess it's implemented using a monad, perhaps the state monad.
Kindly note I'm a beginner in Haskell, so please provide a detailed explanation rather than just linking to the source.
解决方案
This is a simplified example of how a program might keep a history of commands entered by the user. It basically has the same structure as the number guessing game, so once you understand that you should have no trouble understanding this:
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 []
这篇关于用ghci记录历史的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文