【问题标题】:Output putStrLn throws Error when running in the command line在命令行中运行时输出 putStrLn 抛出错误
【发布时间】:2018-12-01 01:33:47
【问题描述】:

我不明白为什么这段代码没有输出。我不断收到错误提示:解析错误(可能不正确的缩进或不匹配的括号) putStrLn "输入多少个数字:"

 main = do
    putStrLn "Enter how many numbers:" -- clearer
    listlen <- getLine 
    if (listlen < 100)
    then 
    putStrLn "Enter a number: "
    numberString <- getLine
    let numberInt =(read numberString :: Int)
    print (numberInt)
    else 
        putStrLn " Error: listlen must be less than 100"

【问题讨论】:

  • 您收到错误的行号是多少?
  • 它说。 test3.hs:4:23 错误:

标签: haskell


【解决方案1】:

当我将您的示例粘贴到我的编辑器中时,失败的那一行是“then”之后的那一行。如果您在“then”之后添加“do”,该错误就会消失。

所以:

then do
putStrLn "Enter a number: "

...等等。

【讨论】:

  • 非常感谢!这消除了我对此的困惑。我现在有以下错误:test3.hs.4:17: error: * no instant for Num String from the literal '100'
  • getLineIO String 类型,即:将行作为String 返回(在 IO monad 内)。所以它不会返回一个数字。所以你不能比较listlen &lt; 100。您需要将String 转换为数字:使用read
【解决方案2】:

listlenString 类型,无法与 100 进行比较。它需要使用read 将其转换为数字:

if (read listlen < 100)
... 

【讨论】:

    猜你喜欢
    • 2015-10-09
    • 2019-07-01
    • 1970-01-01
    • 2021-09-16
    • 1970-01-01
    • 2021-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多