Reputation: 23
I just started learning Haskell, and i'm on stuck on this error among a bunch of others
i'm trying to print all the characters in the char list recursively with this code
printall :: [Char] -> [Char]
printall "" = ""
printall (i:is) = if is /= "" then print i else printall is
main = printall "hello world"
but i get this error could anyone help me?
intro.hs:14:36: error:
• Couldn't match expected type ‘[Char]’ with actual type
‘IO ()’
• In the expression: print i
In the expression: if is /= "" then print i else printa
ll is
In an equation for ‘printall’:
printall (i : is) = if is /= "" then print i else p
rintall is
intro.hs:16:1: error:
• Couldn't match expected type ‘IO t0’ with actual type ‘
[Char]’
Upvotes: 2
Views: 2997
Reputation: 749
As you said in a comment above, each branch of the if clause should have the same type, indeed.
Also, main
function must always have IO a
type, for some a
, which is usually ()
. This means that the type signature for printall
should be:
printall :: [Char] -> IO ()
which is the same as:
printall :: String -> IO ()
Upvotes: 3
Reputation: 2411
print
type is print :: Show a => a -> IO ()
but your printall
type is printall :: [Char] -> [Char]
, assume it is what you want.
The expression if x then y else z
need the same type for y
and z
Upvotes: 0