Consider this code:
type Graph = (Char, Function)
type Function = Char -> Int
doThingsWithGraph :: Graph -> String
func :: Function
graph :: Graph
main = do x <- getChar
y <- getChar
let graph = (x, func)
putStrLn $ doThingsWithGraph graph
The implementation of doThingsWithGraph
does not matter for now. Now the issue here is that the func
's output also depends on the second char I am getting from the user, but I have no idea how to pass it on to it. I can't pass it as an argument because func is implemented by third party and I can't change the type. So if haskell had global variables and y
was one the func
's implementation would look something like this:
func char
|isDigit char = digitToInt char + digitToInt y
|otherwise = digitToInt y
This obviously doesn't work because func
has no idea what y
is, but that's the idea of what it should do. So y
is the value that I need somehow to let func
know about (it is the one I'm getting from user in the second line of main
). I can't do getLine
inside func
because that won't be consistent with data type which I can't change (part of the large framework). Any ideas? Maybe some helper function can replace func
in let graph = (x, func)
and then return the function of type Function? This is just a guess..
func
is implemented by third party". Do you mean "I want third parties to be able to implement suchfunc
s"? – Tom Ellis