用ghci记录历史 [英] Keeping track of history in ghci

查看:121
本文介绍了用ghci记录历史的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

历史管理如何在GHCI或其他基于Haskell的REPL中工作?由于Haskell是一种纯粹的语言,我想它是用monad实现的,也许是 state monad 。请注意,我是Haskell的初学者,所以请提供详细的解释,而不仅仅是链接到源代码。

$ b
$ 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆