- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用Haskeline编写一个程序,询问用户一系列问题,每个问题都可以选择在[括号]中包含一个默认值,并读取他们的回答。我希望用户能够
我已经能够使第 1-3 点工作,但无法使第 4 点工作:按 Ctrl-D(或输入“退出”)只会弹出下一个提示,而不是使程序退出提问。看看我的程序(请参见下文),我明白为什么会发生这种情况,但我无法弄清楚如何解决这个问题,以便 Ctrl-D (或“退出”)实际上使提问停止。我该如何修复程序才能实现这一点?
我确实看到了this question这似乎问了类似的问题,但我无法从那里得到太多;我什至不确定他们是否在问与我相同的问题。
作为第二个问题:我当前的程序有很多 case
语句,它们会打开 Maybe
值。特别是,我目前检查两到三层深度的 Nothing
,以便当用户按 Ctrl-D 时我可以正确返回 Nothing
。我有一种感觉,这可以使用(类似的)单子(monad) >>=
运算符来简化,但我无法弄清楚在这种情况下如何做到这一点。我的预感对吗?有没有办法消除所有这些寻找 Nothing
的模式匹配?
另外:请告诉我任何其他可以改进我的代码的事情。我对此很陌生,所以很可能我在这里遗漏了许多明显的东西。
我的程序询问用户水果篮的组成。与水果篮相关联的信息包括水果篮所有者的姓名以及篮子中不同种类水果的名称。为了能够询问后者,我首先询问篮子中不同种类水果的数量,然后询问每种水果的名称。我们从默认的水果篮开始,然后根据用户告诉我们的内容修改其信息。
module Main where
import System.Console.Haskeline
type PersonName = String
type FruitName = String
data FruitBasket = FruitBasket { ownerName :: PersonName,
fruitCount :: Int,
fruitNames :: [FruitName]
} deriving Show
defaultBasket = FruitBasket "Mary" 2 ["Apple", "Peach"]
main :: IO ()
main = do
basket <- getBasketData defaultBasket
putStrLn $ "Got: " ++ show(basket)
-- Prompt the user for information about a fruit basket, and
-- return a FruitBasket instance containing this information. The
-- first argument is an instance of FruitBasket from which we get
-- the default values for the various prompts. The return value
-- has a Maybe type because the user may abort the questioning, in
-- which case we get nothing from them.
getBasketData :: FruitBasket -> IO (Maybe FruitBasket)
getBasketData basket = runInputT defaultSettings $ withInterrupt $ getData basket
where
getData :: FruitBasket -> InputT IO (Maybe FruitBasket)
getData initialBasket = handleInterrupt f $ do
outputStrLn banner
input <- getInputLine $ "Who owns this basket? [" ++ defaultOwner ++ "] : "
basket <- case input of
Nothing -> return Nothing -- User pressed Ctrl-D with the input being empty
Just "" -> return (Just initialBasket) -- User pressed Enter with the input being empty
Just "quit" -> return Nothing -- User typed in "quit" and pressed Enter
Just newOwner -> return (Just initialBasket{ownerName = newOwner})
input <- getInputLine $ "Number of kinds of fruit in the basket? [" ++ show defaultCount ++ "] : "
basket' <- case input of
Nothing -> return Nothing
Just "" -> return basket
Just "quit" -> return Nothing
Just count -> return $ updateFruitCount basket (read count)
where updateFruitCount Nothing _ = Nothing
updateFruitCount (Just realBasket) newCount = Just $ realBasket{fruitCount = newCount}
let defaultFruitNames = pruneOrPadNames basket'
newNames <- getFruitNames defaultFruitNames 1
case newNames of
Nothing -> return (Just defaultBasket)
Just newSetOfNames -> return $ updateFruitNames basket' newSetOfNames
where updateFruitNames Nothing _ = Nothing
updateFruitNames (Just realBasket) realNewNames = Just $ realBasket{fruitNames = realNewNames}
where f = (outputStrLn "Press Ctrl-D or enter \"quit\" to quit." >> getData initialBasket)
defaultOwner = ownerName initialBasket
defaultCount = fruitCount initialBasket
banner :: String
banner = "Please enter details of the fruit basket below. At each prompt you can do one of the following:\n\
\\t (a) Press Enter to submit the [default] value;\n\
\\t (b) Type in a string, edit it if needed, and then press Enter to submit this value;\n\
\\t (c) Press Ctrl-C to reset all values to the defaults and start over;\n\
\\t (d) Press Ctrl-D or enter \"quit\" to quit; all the values you submitted will be lost."
pruneOrPadNames :: Maybe FruitBasket -> Maybe [String]
pruneOrPadNames Nothing = Nothing
pruneOrPadNames (Just basket) = Just $ pruneOrPad (fruitNames basket) (fruitCount basket)
-- When requiredLength is not larger than (length inputList),
-- (pruneOrPad inputList requiredLength) is the prefix of
-- inputList of length requiredLength. Otherwise, it is inputList
-- padded with as many empty strings as required to make the total
-- length equal to requiredLength.
pruneOrPad :: [String] -> Int -> [String]
pruneOrPad inputList requiredLength
| requiredLength <= inputLength = take requiredLength inputList
| otherwise = inputList ++ (replicate difference "")
where inputLength = length inputList
difference = requiredLength - inputLength
getFruitNames Nothing _ = return Nothing
getFruitNames (Just []) _ = return $ Just [""]
getFruitNames (Just (name:names)) count = do
input <- getInputLine $ "Name of fruit " ++ (show count) ++ " [" ++ name ++ "] : "
newNames <- case input of
Nothing -> return Nothing
Just "" -> do -- Keep the default name for this fruit ...
newNames' <- getFruitNames (Just names) (count + 1)
case newNames' of
Nothing -> return Nothing
-- ... unless the user chose to quit
-- while entering a name
Just [""] -> return $ Just [name]
-- At this point names = [] so it is
-- already time to stop asking for
-- more names.
Just furtherNames -> return $ Just (name : furtherNames)
Just "quit" -> return Nothing
Just name' -> do
newNames' <- getFruitNames (Just names) (count + 1)
case newNames' of
Nothing -> return Nothing
Just [""] -> return $ Just [name']
Just furtherNames -> return $ Just (name' : furtherNames)
return newNames
最佳答案
在一些建议的帮助下here on the haskell-beginners mailing list我已经成功地解决了我的问题,完全解决了 Ctrl-D 问题和因式分解问题,令我满意(到目前为止!)。我将答案发布在这里,希望它能帮助其他人解决我的困境。
首先,Ctrl-D 的问题:问题是我抛弃了 Maybe monad 提供的控制逻辑,只使用了值 monad,通过引用包含这些值的各种变量名称。我执行此操作的第一个地方是这里,在 getBasketData
中。功能:
basket <- case input of ...
input <- getInputLine ...
basket' <- case input of
Nothing -> return Nothing
Just "" -> return basket
注意计算中的情况 basket'
,我
basket
的情况可能是Nothing
,并且basket
封装的值通过引用(并在需要时进行模式匹配)变量 basket
它仍然在 basket'
表达式的范围内.这就是 Ctrl-D 丢失的地方。作为对比,这里的代码是 getBasketData
这不让Nothing
s 穿过间隙(我将 basket
变量重命名为 maybeBasket
,因为它们实际上是 Maybe FruitBasket
的实例):
getBasketData basket = runInputT defaultSettings $ withInterrupt $ getData basket
where
getData :: FruitBasket -> InputT IO (Maybe FruitBasket)
getData initialBasket = handleInterrupt f $ do
outputStrLn banner
input <- getInputLine $ "Who owns this basket? [" ++ defaultOwner ++ "] : "
maybeBasket <- case input of
Nothing -> return $ Nothing -- User pressed Ctrl-D with the input being empty
Just "" -> return $ Just initialBasket -- User pressed Enter with the input being empty
Just "quit" -> return $ Nothing -- User typed in "quit" and pressed Enter
Just newOwner -> return $ Just initialBasket{ownerName = newOwner}
maybeBasket' <- case maybeBasket of
Nothing -> return $ Nothing
Just realBasket -> do input <- getInputLine $ "Number of kinds of fruit in the basket? [" ++ show defaultCount ++ "] : "
case input of
Nothing -> return $ Nothing
Just "" -> return $ maybeBasket
Just "quit" -> return $ Nothing
Just count -> return $ Just $ realBasket{fruitCount = (read count)}
maybeBasket'' <- case maybeBasket' of
Nothing -> return $ Nothing
Just realBasket -> do let defaultFruitNames = pruneOrPad (fruitNames realBasket) (fruitCount realBasket)
newNames <- getFruitNames defaultFruitNames 1
case newNames of
Nothing -> return $ Nothing
Just newSetOfNames -> return $ Just $ realBasket{fruitNames = newSetOfNames}
return maybeBasket''
where f = (outputStrLn interruptMessage >> getData initialBasket)
defaultOwner = ownerName initialBasket
defaultCount = fruitCount initialBasket
因此,例如,我们尝试进行任何实际计算以获得 maybeBasket'
--- 包括提示不同种类水果的数量 --- 仅当maybeBasket
时不是Nothing
。
这解决了 Ctrl-D 问题:程序现在停止询问并返回 Nothing
如果用户按 Ctrl-D 来回答任何问题。
现在进行保理。这就是邮件列表答案中的建议有所帮助的地方:我首先将大的 getData
分开。函数分为三部分,每个“大”使用 <-
运算符,并将这些部分放入单独的函数中。这对我来说逻辑清晰了很多(事实上,这也是我找到解决 Ctrl-D 问题的方法)。从这里开始,我不断地重新表述各个部分,直到得到对我来说看起来足够好的以下版本。请注意getBasketData
是多么小和干净。功能变成了!
module Main where
import System.Console.Haskeline
type PersonName = String
type FruitName = String
data FruitBasket = FruitBasket { ownerName :: PersonName,
fruitCount :: Int,
fruitNames :: [FruitName]
} deriving Show
defaultBasket :: FruitBasket
defaultBasket = FruitBasket "Mary" 2 ["Apple", "Peach"]
main :: IO ()
main = do
basket <- getBasketData defaultBasket
putStrLn $ "Got: " ++ show(basket)
-- Prompt the user for information about a fruit basket, and
-- return a FruitBasket instance containing this information. The
-- first argument is an instance of FruitBasket from which we get
-- the default values for the various prompts. The return value
-- has a Maybe type because the user may abort the questioning, in
-- which case we get nothing from them.
getBasketData :: FruitBasket -> IO (Maybe FruitBasket)
getBasketData basket = runInputT defaultSettings $ withInterrupt $ getData basket
where
getData :: FruitBasket -> InputT IO (Maybe FruitBasket)
getData initialBasket = handleInterrupt f $ do
outputStrLn banner
(ownerQ initialBasket) >>= (processOwner initialBasket) >>= processCount >>= processNames
where f = (outputStrLn interruptMessage >> getData initialBasket)
ownerQ :: FruitBasket -> InputT IO (Maybe PersonName)
ownerQ basket = getInputLine $ "Who owns this basket? [" ++ (ownerName basket) ++ "] : "
processOwner :: FruitBasket -> Maybe PersonName -> InputT IO (Maybe FruitBasket)
processOwner _ Nothing = return Nothing
processOwner _ (Just "quit") = return Nothing
processOwner basket (Just "") = return $ Just basket
processOwner basket (Just newOwner) = return $ Just basket{ownerName = newOwner}
processCount :: Maybe FruitBasket -> InputT IO (Maybe FruitBasket)
processCount Nothing = return Nothing
processCount (Just basket) = (fruitTypesQ basket) >>= processCount'
where processCount' :: Maybe String -> InputT IO (Maybe FruitBasket)
processCount' Nothing = return Nothing
processCount' (Just "quit") = return Nothing
processCount' (Just "") = return $ Just basket
processCount' (Just count) = return $ Just basket{fruitCount = (read count)}
fruitTypesQ :: FruitBasket -> InputT IO (Maybe String)
fruitTypesQ basket = getInputLine $ "Number of kinds of fruit in the basket? [" ++ show (fruitCount basket) ++ "] : "
processNames :: Maybe FruitBasket -> InputT IO (Maybe FruitBasket)
processNames Nothing = return Nothing
processNames (Just basket) = input >>= processNames'
where input = getFruitNames defaultFruitNames 1
defaultFruitNames = pruneOrPad (fruitNames basket) (fruitCount basket)
processNames' :: Maybe [FruitName] -> InputT IO (Maybe FruitBasket)
processNames' Nothing = return Nothing
processNames' (Just newSetOfNames) = return $ Just basket{fruitNames = newSetOfNames}
banner :: String
banner = "Please enter details of the fruit basket below. At each prompt you can do one of the following:\n\
\\t (a) Press Enter to submit the [default] value;\n\
\\t (b) Type in a string, edit it if needed, and then press Enter to submit this value;\n\
\\t (c) Press Ctrl-C to reset all values to the defaults and start over;\n\
\\t (d) Press Ctrl-D or enter \"quit\" to quit; all the values you submitted will be lost."
interruptMessage :: String
interruptMessage = "#################################################\n\
\You pressed Ctrl-C.\n\
\We will now reset all values and start over.\n\
\To quit, press Ctrl-D or enter \"quit\".\n\
\#################################################\n"
pruneOrPadNames :: Maybe FruitBasket -> Maybe [String]
pruneOrPadNames Nothing = Nothing
pruneOrPadNames (Just basket) = Just $ pruneOrPad (fruitNames basket) (fruitCount basket)
-- When requiredLength is not larger than (length inputList),
-- (pruneOrPad inputList requiredLength) is the prefix of
-- inputList of length requiredLength. Otherwise, it is inputList
-- padded with as many empty strings as required to make the total
-- length equal to requiredLength.
pruneOrPad :: [String] -> Int -> [String]
pruneOrPad inputList requiredLength
| requiredLength <= inputLength = take requiredLength inputList
| otherwise = inputList ++ (replicate difference "")
where inputLength = length inputList
difference = requiredLength - inputLength
getFruitNames :: [FruitName] -> Int -> InputT IO (Maybe [FruitName])
getFruitNames [] _ = return $ Just [""]
getFruitNames (name:names) count = do
input <- getInputLine $ "Name of fruit " ++ (show count) ++ " [" ++ name ++ "] : "
newNames <- case input of
Nothing -> return Nothing
Just "" -> do -- Keep the default name for this fruit ...
newNames' <- getFruitNames names (count + 1)
case newNames' of
Nothing -> return Nothing
-- ... unless the user chose to quit
-- while entering a name
Just [""] -> return $ Just [name]
-- At this point names = [] so it is
-- already time to stop asking for
-- more names.
Just furtherNames -> return $ Just (name : furtherNames)
Just "quit" -> return Nothing
Just name' -> do
newNames' <- getFruitNames names (count + 1)
case newNames' of
Nothing -> return Nothing
Just [""] -> return $ Just [name']
Just furtherNames -> return $ Just (name' : furtherNames)
return newNames
<小时/>
这个故事的寓意似乎是:“当困惑时,把事情分解。”
关于haskell - 让 Haskeline 提前退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29189428/
如果这有点含糊,我提前道歉,但我什至缺乏如何处理这个问题的基本想法 - 甚至不知道是否有合适的术语可供搜索。 我正在尝试编写一个按时间顺序排列的动画事件的表格驱动系统,其中描述性评论也从表格中提取出来
我是一个老狗,30 年前就用过 BASIC >。我以前在 python 中使用 for 循环时遇到过这种情况,但我选择这个例子是因为我担心循环: 我想解析一个长字符串,其中包含用逗号分隔的双引号中的单
我需要获取从当天算起的 5 个工作日的数组。 今天是:06/04/2018 我需要的输出是: { 0: 06/01/2018, //fri. 1: 05/31/2018, //th
我习惯于使用时间戳,我现在将尝试使用正常日期 2011-02-02 12:00:00 格式 我有这个: SELECT * FROM users_calenders WHERE date ? 我想选择日
我在我的本地仓库 (test-branch) 中创建了一个测试分支,并将其推送到 Github。 如果我转到我的 Github 帐户并选择这个 test-branch 它会显示信息: This bra
我正在尝试设置 JSON 对象的结束日期。结束日期等于开始日期后 30 天。有时这会返回正确的日期,有时则不会。 这是GetDateSchedulerFormatted函数 GetDateSchedu
我有一个执行器服务,它定期执行一堆任务。它们在启动时初始化并经常运行,到目前为止一切顺利。 我现在想添加功能来根据事件快速启动这些任务的执行。 我找到了decorateTask方法,它允许我存储我安排
我需要比当前日期提前 3 周的日期。并将所有日期添加到数组中。我怎样才能得到这个? let date = NSDate() let calendar = NSCalendar(cal
我正在使用以下代码设置日期对象: NSDate *date = [NSDate date]; NSCalendar *gregorian = [[NSCalendar alloc] i
我正在将字符串“Jun 11, 2012 9:30 PM”转换为 NSDate,并且由于某种原因我一直提前 4 小时。有趣的是,我正在使用相同的字符串在详细 View 中提供 UIDatePicker
我的理解是 Xamarin 的提前 (AOT) 编译器将 Xamarin.iOS 应用程序直接编译为 native ARM 汇编代码 (How Xamarin works) . 然而,我不明白的是为什
Angular 2 带有称为提前 (AoT) 的新功能。但是看了一番,还是不能真正理解。它是如何工作的?它将如何带来更好的性能?它与 JIT 有何不同? 谢谢。 最佳答案 Angular 在模块、指令
我看到了一些关于如何纠正这个问题的答案。我有一个 DateTime 类型的对象。我已分配该对象,如下所示。 obj.TimeStamp = DateTime.UtcNow; 我似乎找不到正确的组合或代
我是 Fortran 新手,我不明白这一行: write(*,'(a35)', advance='no') 在: program democonvertion implicit none
我一直在寻找如何做一些像 facebook 新闻提要这样的“高级”列表,但我认为我没有使用正确的关键字来搜索如何做到这一点。我对 android 环境还是很陌生。 这就是我要实现的目标: 我怎样才能得
我有一个包含 2 列的 pandas Dataframe。其中一个是日期格式的索引,另一个是比率 R(0 到 1 之间的数字)。如何向 pandas Dataframe 添加另一列,其中包含一天前一周
我有 2 个媒体查询大小 - only screen and (min-width: 980px)and (max-width: 1499px)"; only screen and (min-widt
我发现了这个: Is AOT (ahead of time) compilation available (or planned) in mono for android? 但是这个问题很老了。 在单
在我看来,当我调用 JTree.expandPath( path ) 默认情况下,它的所有父级也会展开。但我真正想做的是,设置特定的隐形 child 提前展开。这样当一个节点展开时,它的完整子树就会弹
我的时差显示不正确的输出,我正在尝试计算 startTime 和 endTime 之间的时差。 Date time1, time2; long difference; Simp
我是一名优秀的程序员,十分优秀!