Let's say I have a higher order function which performs some computation using values which it retrieves from it's functional parameter.
f :: a -> (b -> c) -> d
where a,b,c,d are some concrete types.
Then I have a function with this type
g :: b -> m c
where m is some monad.
Now is there a way to use g with f. That is turn f into a function which produces m d instead of d and can use g as it's second parameter?
A concrete example would be that m is the IO monad, f being a function computing sum of n numbers retrieved from its functional parameter and g reads a number from standard input.
f n g = sum $ map g (1..n)
g :: Int -> IO Int
g _ = readLn
I know there are functions for converting the standard input into a lazy list, which would solve this problem but my real situation is not that simple.
Suppose I have an algorithm for doing something on a graph. The algorithm uses a functional parameter to determine the neighbours of a node. This is so that I can have multiple implementations of the graph.
Now I want to use this algorithm with a non-deterministic graph (List monad) or a graph that is not fully known (Maybe monad). I know I could rewrite the algorithm to use monads and then use the identity monad for the basic case, but is this the only way? I think it would be easier to write the algorithm without monads.
Is this behaviour possible? I couldn't find a reason why it shouldn't be but I was not able to find a way how to do it.