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")
我的最后一行让编译器错误。怎么了?
答案 0 :(得分:5)
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")