Here’s the type error I am getting, followed by the code in question. This may be due to an incorrect use of function composition, if so I’d like an explanation as to how I can fix that. If it’s something else, I’d like to talk about that too.
frameworkDaemon.lhs:125:5:
Couldn't match expected type `IO ()' with actual type `a0 -> c0'
In the expression: popJobState . popProcessConfig . readJobFile
In an equation for `makeJobState':
makeJobState world
= popJobState . popProcessConfig . readJobFile
where
readJobFile
= do { let ...;
.... }
where
getFileContents (x : xs) = readFile (ixiaRoot ++ "/" ++ (show x))
popProcessConfig = undefined
popJobState = undefined
Failed, modules loaded: none.
makeJobState :: JobState -> IO ()
makeJobState world =
popJobState . popProcessConfig . readJobFile -- f .g . h -> f(g(h))
where readJobFile = do
let directoryPath = ixiaRoot ++ "/jobs"
processedPath = map (directoryPath </>) . filter (`notElem` [".",".."])
jobFileNames <- processedPath <$> getDirectoryContents directoryPath
let numStrings = map (last . splitOn "/") jobFileNames
sortJobNumbers = map read numStrings :: [Int]
getFileContents $ sort $ sortJobNumbers
where getFileContents (x:xs) = readFile (ixiaRoot ++ "/" ++ (show x))
popProcessConfig = undefined
popJobState = undefined
As sepp2k said,
f.g.his a function,\x -> f $ g $ h x. It seems thatreadJobFileisIO String, though, notIO ().But it doesn’t complain about expecting a function. It explains about having inferred a function, and having expected an
IO (). It’s inferred a function because your chain of compositions is just that—a function, so the result of applyingmakeJobStateto aJobStatevalue is (or would be) a function, not an IO action.It seems as if what you want isn’t function composition but monadic composition, something like
makeJobState world = readJobFile >>= popProcessConfig >>= popJobState(with theworldparameter actually being used at some point, though it’s not really obvious to me where it should go. And that might not be the preferred order; it’s hard to tell since the other two variables are undefined.) SincereadJobFileisIO String, you’d wantpopProcessConfigto beString -> IO aandpopJobStateto bea -> IO (). That would result in an overall type ofJobState -> IO ().