Haskell type problem

Code example:

fac :: Int → Int fac 0 = 1 fac n = n * fac (n-1) main = do putStrLn show fac 10 

Error:

 Couldnt match expected type 'String' against inferred type 'a -> String' In the first argument of 'putStrLn', namely 'show' In the expression: putStrLn show fac 10 
+4
source share
1 answer

Add in parentheses to show how this code actually parses:

 (((putStrLn show) fac) 10) 

You pass show as an argument to putStrLn , which is incorrect because show is a function and putStrLn expects a string. You want it to be like this:

 putStrLn (show (fac 10)) 

You can either bracket it like this, or you can use the $ operator, which essentially braces everything that is to the right of it:

 putStrLn $ show $ fac 10 
+25
source

All Articles