haskell我的最后一行“putStr"扔错了

时间:2014-11-30 07:40:41

标签: 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")

我的最后一行让编译器错误。怎么了?

2 个答案:

答案 0 :(得分: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")

答案 1 :(得分:0)

从您的缩进中,您似乎正在尝试用do符号书写。只需添加关键字do即可修复您的代码:

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

     putStr (encode 3 "hey")