BufBills
BufBills

Reputation: 8113

haskell, why does head' function okay in ghci but cannot be compiled?

head' :: [a] -> a
head' [] = error "Cannot call head on emply list."
head' (x:_) = x 

main = do  
    putStrLn  "hello"
    let ret = head' [4,5,6]
    putStrLn ret 

Above code, I can load it in ghci and call head' function correctly. When I put it into a file and try to compile it. IT thows error. I cannot figure out why is that. need help; thanks.

[1 of 1] Compiling Main             ( head.hs, head.o )

head.hs:7:22:
    No instance for (Num String) arising from the literal `4'
    Possible fix: add an instance declaration for (Num String)
    In the expression: 4
    In the first argument of head', namely `[4, 5, 6]'
    In the expression: head' [4, 5, 6]

Upvotes: 1

Views: 140

Answers (1)

David Young
David Young

Reputation: 10793

putStrLn takes a String (its type is putStrLn :: String -> IO ()), not a numeric type. What you want is print

    ...
    print ret

print can take any type that has a Show instance. Its type is print :: Show a => a -> IO ()

Upvotes: 4

Related Questions