haskell我的最后一行"putStr"引发了错误

Buf*_*lls 0 haskell

import Data.List
import Data.Char

isIn :: (Eq a) => [a] -> [a] -> Bool
needle `isIn` haystack = any (needle `isPrefixOf` ) (tails haystack)


encode :: Int -> String -> String
encode offset msg = map (\c -> chr $ ord c + offset) msg

main :: IO()
main =
     if "arts" `isIn` "artsisgood" then 
        putStrLn "is in"
     else
        putStrLn "not in"

     putStr (encode 3 "hey")
Run Code Online (Sandbox Code Playgroud)

我的最后一行让编译器错误.它出什么问题了?

小智 5

2个问题:

  • 缩进对你的if语句不好
  • 你没有链接你的2个操作(见下面的例子)

你的代码修好了:

import Data.List
import Data.Char

isIn :: (Eq a) => [a] -> [a] -> Bool
needle `isIn` haystack = any (needle `isPrefixOf` ) (tails haystack)


encode :: Int -> String -> String
encode offset = map (\c -> chr $ ord c + offset)
-- encode offset msg = map (\c -> chr $ ord c + offset) msg

main :: IO()
main = do
     if "arts" `isIn` "artsisgood" 
       then putStrLn "is in"
       else putStrLn "not in"
     putStr (encode 3 "hey")

main2 =
   if "arts" `isIn` "artsisgood" 
   then putStrLn "is in"
   else putStrLn "not in"
   >> putStr (encode 3 "hey")
Run Code Online (Sandbox Code Playgroud)