[Haskell-cafe] FFI basics

Bulat Ziganshin bulat.ziganshin at gmail.com
Fri Feb 9 08:43:48 EST 2007

Hello Yitzchak,

Friday, February 9, 2007, 3:23:53 PM, you wrote:

> I would like to use FFI for the first time. Can someone
> give me a really, really simple complete example?

nothing can be easier

main = print (c_mysin 1.0)

foreign import ccall "mysin.h mysin"
  c_mysin :: Double -> Double


double mysin(double x)
  return sin(x);


double mysin(double x);

ghc -c mysin.c
ghc main.hs mysin.o

if you need to call imperative function (wjich returns different
values for different calls with the same parameters) - just add IO to
return type:

foreign import ccall "myrnd.h rnd"
  c_rnd :: Double -> IO Double

in C, function defined in the same way as in previous example

look also at "Tackling the awkward squad: monadic input/output,
concurrency, exceptions, and foreign-language calls in Haskell"

Best regards,
 Bulat                            mailto:Bulat.Ziganshin at gmail.com

More information about the Haskell-Cafe mailing list