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

Experimenting with GHCi

Before we begin, we need to make our C code accessible. All we have at the moment is some text files. Let’s compile them into something we can use.

 $ gcc -shared caesar.c -o caesar.so
 # Depending on your OS and compiler version
 # you may need to do the following
 $ gcc -fPIC -shared caesar.c -o caesar.so
 # You may also need to set LD_LIBRARY_PATH as well
 $ export LD_LIBRARY_PATH=.

We can now invoke GHCi and try out our code.

 $ stack exec ghci caesar.hs caesar.so
 GHCi, version 7.10.3: http://www.haskell.org/ghc/ :? for help
 [1 of 1] Compiling Main ( caesar.hs, interpreted )
 Ok, modules loaded: Main.
 *Main> caesar 2 "ATTACKATDAWN"
 "CVVCEMCVFCYP"
 *Main> native_caesar 2 "ATTACKATDAWN"
 "CVVCEMCVFCYP"

We can ...

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