简体   繁体   English

Haskell - 将字符串输入保存到列表中

[英]Haskell - Saving string input into a list

I'm a Haskell beginner and I am doing a small file for a project that should take the input of interaction data for groups of two people and save it to a list to be output at the end.我是一个 Haskell 初学者,我正在为一个项目做一个小文件,该项目应该输入两个人组的交互数据并将其保存到一个列表中,最后是 output。 I have done my best to implement this but it seems like the program hits the "stop" case no matter what is input.我已尽我所能来实现这一点,但无论输入什么,程序似乎都会遇到“停止”情况。 Any help or advice would be appreciated.任何帮助或建议将不胜感激。

import Data.List
import Text.Read

main :: IO ()
main = do
    putStrLn "This program is a means to record interactions between individuals during the COVID-19 pandemic."
    putStrLn "Please enter your interactions in this format: 'x interacted with y'"
    inputs <- getUserInputs
    putStr "input: "
    putStrLn ("list sequence " ++ show (inputs))

parseInput :: String -> Maybe String
parseInput input = if input == "stop" then Nothing else (readMaybe input):: Maybe String

getUserInputs :: IO [String]
getUserInputs = do 
    input <- getLine
    case parseInput input of
    Nothing -> return []
    Just aString -> do
        moreinputs <- getUserInputs
        return (aString : moreinputs)

Show and Read are intended to produce and consume the representation of a value as a Haskell expression. ShowRead旨在将值的表示形式生成和使用为 Haskell 表达式。 That's why when you call show on a String , it produces a quoted string:这就是为什么当您在String上调用show时,它会产生一个带引号的字符串:

> show "beans"
"\"beans\""

Therefore Read expects the string to be quoted as well, so readMaybe is always returning Nothing in your code because you're not supplying quotes:因此Read期望字符串也被引用,所以readMaybe总是在你的代码中返回Nothing因为你没有提供引号:

> readMaybe "beans" :: Maybe String
Nothing

> readMaybe "\"beans\"" :: Maybe String
Just "beans"

Therefore the fix is simple: remove the call to readMaybe and just return the string directly:因此修复很简单:删除对readMaybe的调用并直接返回字符串:

parseInput1 :: String -> Maybe String
parseInput1 input = if input == "stop"
  then Nothing
  else Just input

Which, as a matter of style preference, you could also write with guards, pattern matching, or the Maybe monad instead of if :根据风格偏好,您还可以使用警卫、模式匹配或Maybe monad 来代替if

parseInput2 input
  | input == "stop" = Nothing
  | otherwise = Just input
parseInput3 "stop" = Nothing
parseInput3 input = Just input
import Control.Monad (guard)

parseInput4 input = do
  -- ‘guard’ returns ‘Nothing’,
  -- short-circuiting the ‘do’ block,
  -- if its condition is ‘False’.
  guard (input /= "stop")
  pure input

Read and Show are fine for simple programs, particularly when you're learning Haskell, but in larger applications it's helpful to use them mostly for debug input and output and read ing input you've already validated. ReadShow对于简单的程序来说很好,尤其是在您学习 Haskell 时,但在较大的应用程序中,将它们主要用于调试输入和 output 以及您已经验证的read输入会很有帮助。 Parsing and pretty-printing libraries are preferable for more involved parsing and producing human-readable output, respectively;解析库和漂亮打印库分别用于更多涉及的解析和生成人类可读的 output; megaparsec and prettyprinter are good default choices in that area. megaparsecprettyprinter是该领域中不错的默认选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM