Interactive & Literate Programming
Table of Contents
1. Get Haskell Babel Working
This seemed to be both easier and harder as I started getting more comfortable with emacs.
Simply installing haskell (through your distro package manager, for me it was windblows using chocolately) & haskell-mode M-x package-install RET haskell-mode
should have been enough to get me going, since org-babel is installed by default on versions 24+.
Then I ran into the same error message as before
org-babel-execute-src-block: No org-babel-execute function for haskell!
which doesn't give me a lot to go on. I tested this in powershell to make sure I could run GHCI (I can), then started looking into what the various haskell things look like under the hood of emacs.
Still not sure what I did at the very end to change things but after I restarted emacs, and executed the block below, I got the error message buffer *​haskell* does not exist or has no process
. I checked M-x list-buffers
and it showed the *​haskell* buffer. Once I viewed this buffer it showed an error message and abnormal return code of 1, but also showed some weird error path around the hakyll repo I'm in. I killed this buffer, then restarted emacs. I ran the code block on a .org
file in a non-hakyll directory and it worked just fine. Then navigated to this post, and it ran fine again. I was able to recreate this bug by restarting emacs, running this post's first to the same buffer...
error (and then restarted and back again).
2. Evaluating my first code inline
I am going to print the standard "Hello, World!" output, then demonstrate a basic function.
putStrLn "Hello, World!" doubleMe x = x + x doubleMe 9
3. Starting Literate Programming
According to Wikipedia, "Literate programming is writing out the program logic in a human language with included (separated by a primitive markup) code snippets and macros." This is something like above, where I can describe what I want to do in my native language, try to implement that action with the code, evaluate the code, and move on to the next task. Ideally, this would also help me return to the code later, and if it were to expand to a longer program, through the process of tangling, can create the source from the literate programming document.
Using Literate Programming can be useful for a number of things:
- Haskell is semi-literate to begin with, meaning that it can natively compile the lierate extension with code blocks written in LaTeX and will ignore the LaTeX.
- Using a literate org file as the emacs initialization with the emacs lisp language
- Learning new programming languages and concepts in a notetaking style
- Howardim's Literate Programming Tutorial - helped me make sense of variables and block to block passing
Some potential applicatiosn I'm considering, but not sure of the feasibility:
- Template files for things like budgets that would need inputs and computations
- A random adventure generator that uses the output of random tables that then is used to create the new adventure file with the random elements
4. Bonus - Babel as a Meta-Programming Language
I found an interesting bit about Babel being a meta-programming language on the Babel Intro Page. Specifically that output from one function in one language can be used for input into another function of a different language.
When I writing about the three different content interactions, consuming, interacting, formatting, I had really only considered the ability to consume and interact with literate programming. Now that I know org has a robust export feature, it widely can take care about the formatting, but with the ability to computer AND pass things to something like R
for display, it ups the game and ability even more.