O'Reilly logo

Functional Programming: A PragPub Anthology by Michael Swaine

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

A Brief Introduction to Haskell’s FFI

Haskell, like many other languages, has the ability to call native code. It also has an interface for native code to call Haskell, but we aren’t going to explore that in this chapter. We are interested in the ability for Haskell to call our native Caesar cipher implementation.

Haskell’s Foreign Function Interface (FFI) can be invoked using the following construct:

 caesar.hs
 
 foreign import ccall "caesar.h caesar" c_caesar
  :: CInt -> CString -> CString

It starts with foreign import ccall, which signals our call. It’s followed by the location where the function is defined and the name of the function we wish to call. Next we provide a name that we can use for this function inside our Haskell code. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required