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 ...
Get Functional Programming: A PragPub Anthology now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.