I'm learning Haskell and I'm trying to figure out why I'm getting an error on the following piece of code. I'm trying to define a function that can read a file at a given location, and perform some function on it, but it has an error that I'm afraid I can't get my head around.
I imagine I'm doing something stupid but I can't figure out what; can someone tell me?
readAndProcessFile :: String -> (String -> a) -> a
readAndProcessFile l f = do
contents <- readFile l -- error here
let result = f contents
return result
I get the error:
Occurs check: cannot construct the infinite type: a ~ IO a
In a stmt of a 'do' block: contents <- readFile l
In the expression:
do contents <- readFile l
let result = (f contents)
return result
In an equation for ‘readAndProcessFile’:
readAndProcessFile l f
= do contents <- readFile l
let result = ...
return result
• Relevant bindings include
f :: String -> a
readAndProcessFile :: String -> (String -> a) -> a
readAndProcessFIle
isString -> (String -> a) -> IO a
. – Willem Van Onsema
instead ofIO a
? – OliverRadiniIO
is exactly to add a layer to make this impossible (well you can useunsafePerformIO
, but that is, like the name suggests, unsafe). – Willem Van Onsem