Reputation: 8103
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")
My last line makes compiler throw err. What's wrong with it?
Upvotes: 0
Views: 103
Reputation: 3428
From your indentation, it seems like you're trying to write in do
notation. Just adding the keyword do
fixes your code:
main :: IO()
main = do
if "arts" `isIn` "artsisgood" then
putStrLn "is in"
else
putStrLn "not in"
putStr (encode 3 "hey")
Upvotes: 0
Reputation: 124
2 problems:
Your code fixed:
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")
Upvotes: 5