IO in the middle

Serge D. Mechveliani mechvel at
Thu Nov 17 14:19:50 CET 2011


is it possible to arrange a connected output and input (with something 
like a socket) "in a middle" of the Haskell function?
Consider the example of sorting an integer list:
 sortInt :: [Int] -> [Int] 
 sortInt js = 
         let callString = "sortForeign( " ++ (show js) ++ ")" 
             ?      = outputToSocket callStr socket?        -- strict order?
             resStr = inputFromSocket socket?  :: String    -- 
             (res, _) = reads str' :: ([Int], String) 
         in  res

It forms the call string for a foreign function, passes it to a Socket,
the foreign system parses  callString,  evaluates it, and returns the 
string  resStr  for the result (inputFromSocket needs to happen later than 
outputToSocket). Then,  reads  converts it back to [Int].

Forgetting of the pure-functionality philosophy, which items in the Haskell
library or in the GHC library may help? Is the C interface needed?
What if there is a  file  instead of socket?
(for this Foreign is able to apply  readFile, to parse and evaluate the 

I had an experience with  readFile  in Haskell, but this was only the 
top function. Here the situation looks more complex.

Thank you in advance for your explanation.

mechvel at

More information about the Glasgow-haskell-users mailing list