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

Wrapping Our Native Code in Haskell

First things first, we need to provide a missing component. We haven’t yet created our caesar.h file with the definition of our native caesar function. Let’s take care of this before we continue.

 caesar.h
 #pragma once
 
 char *caesar(int shift, char *input);

In order to smooth out the type differences in our code, let’s write a wrapper function. This function will take standard Haskell types and return them, but inside the function, it will convert between the Haskell and C types appropriately so the types don’t pollute the rest of our Haskell code. There is one exception here, but we will get to that soon enough.

 caesar.hs
 native_caesar :: Int -> String -> IO String
 native_caesar shift input = withCString ...

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