如何显示两个输入是否相同

0 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"
Run Code Online (Sandbox Code Playgroud)

Wil*_*sem 8

这些错误可能是由于本地版本与此处发布的版本之间的缩进发生微小变化而引起的.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"
Run Code Online (Sandbox Code Playgroud)

此外,现在你比较两个字符串.您可以将这些转换为Ints(或其他可读类型),例如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"
Run Code Online (Sandbox Code Playgroud)