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]’
printall
, or perhaps just remove it and see what happens. – n. 1.8e9-where's-my-share m.printall "" = ""
line, and alsoprintall (i:is)
line. The latter hasprint i
in thethen
branch andprintall is
in theelse
branch, The types of the two expressions ought to be the same. Are they? – n. 1.8e9-where's-my-share m.[Char]
as the return type. If you want it to do IO and not return a value, useIO ()
as a return type. If you want to both return a string and do IO then useIO [Char]
as a return type. Whatever type you choose, you must be coherent with it in all the branches of your code. – chiprint t
would not return aChar
. It's an IO function. You can check its type by entering:t print
at ghci prompt. – n. 1.8e9-where's-my-share m.