
On Sun, Jun 2, 2013 at 10:19 PM, Ting Lei
Thanks for your answers so far.
It seems that the laziness of String or [char] is the problem.
My question boils then down to this. There are plenty of Haskell FFI examples where simple things like sin/cos in
can be imported into Haskell as pure functions. Is there a way to extend that to String without introducing an IO (), but maybe sacrificing laziness? If String has to be lazy, is there another Haskell data type convertible to String that can do the job? The C++/C function (e.g. toUppers) is computation-only and as pure as cos and tan. The fact that marshaling string incurs an IO monad in current examples is kind of unintuitive and like a bug in design. I don't mind making redundant copies under the hood from one type to another..
Hi Ting, In the Foreign.C.String there is a function that converts String to an array (CString = Ptr CChar) which can be handled on the C side: withCString :: String -> (CString -> IO a) -> IO a peekCString :: CString -> IO String It's slightly more convenient to use these functions through the preprocessor c2hs, as in the following example http://code.haskell.org/~aavogt/c_toUpper_ffi_ex/. c2hs also has a 'pure' keyword which makes it add the unsafePerformIO, but for whatever reason the side-effects were not done in the right order (the peekCString happened before the foreign function was called). Regards, Adam