haskell - 如何显示两个输入是否相同
问题描述
我试图只比较两个用户输入,但我似乎无法让它工作并且不断出现解析错误。任何帮助将不胜感激。
main = do
foo <- putStrLn "Enter two numbers."
numone <- getLine
numtwo <- getLine
putStrLn $ ("You entered " ++ numone ++ " and " ++ numtwo)
if
numone == numtwo
then
putStrLn "They are the same"
else
putStrLn "They are not the same"
解决方案
错误可能是由于本地版本和此处发布的版本之间缩进的微小变化而引起的。Haskell 中的缩进非常重要,因为编译器使用它来理解某些“块”的开始和结束位置。
此外,您可以删除该foo <-
部分(这并没有错,但毫无用处)。所以重新格式化后我们得到:
main = do
putStrLn "Enter two numbers."
numone <- getLine
numtwo <- getLine
putStrLn $ ("You entered " ++ numone ++ " and " ++ numtwo)
if numone == numtwo
then
putStrLn "They are the same"
else
putStrLn "They are not the same"
此外,现在您比较两个字符串。您可以将这些转换为Int
s (或其他可读类型),例如readLn :: Read a => IO a
:
main = do
putStrLn "Enter two numbers."
numone <- readLn :: IO Int
numtwo <- readLn :: IO Int
putStrLn $ ("You entered " ++ show numone ++ " and " ++ show numtwo)
if numone == numtwo
then
putStrLn "They are the same"
else
putStrLn "They are not the same"
推荐阅读
- jquery - 未捕获的类型错误:无法读取 null 的属性“按钮”
- zsh - ZSH“找不到命令:z”
- java - 如何让spring cron作业由spring boot的app用户以外的用户执行
- python - 如何从空文件初始化dict文件代码?
- python - 给定 http 链接的源代码中的 Grep 字符串
- python - CUDA_ERROR_OUT_OF_MEMORY 张量流
- json - Python 和 BeautifulSoup:如何将 JSON 转换为 CSV
- spring-boot - 获取 NullPointer 而不是创建令牌
- apache-spark - 从 PySpark 查询日期之间的 Vertica
- assembly - 第二阶段引导加载程序已加载但未执行