Duly Noted

Simple Code vs Fancy Code (Recurse Day 2)

Originally at https://notes.shaunagm.net/post/643321838697693184/simple-code-vs-fancy-code-recurse-day-2

Building off of yesterday’s work, today my hope was to finish my humble text-focused Madlibs program, so that I could move onto code-focused Madlibs tomorrow.

One of the first things I did today was change the language/dialect I was working in from Common Lisp to Racket. The REPL for Common Lisp was driving me up a wall, and the two suggestions I got from Recursers were switching to Emacs/Slime as an IDE or switching to Racket, which meant a different IDE _and_a slightly different language. At first I was leaning towards Slime, since switching an IDE sounded easer than re-writing all my code, but then I saw how good the Racket documentation was and how nice the IDE looked and changed my mind.

Racket is great and re-writing my code wasn’t a big deal. The core concepts are the same, maybe not for the language as a whole but for the very basic code I wrote, and it was just twenty or thirty minutes debugging minor syntax errors.

I then moved on to trying to iterate on the project. The working version I had at the end of the day yesterday had no abstracted idea of a story. So the next step was creating a program that took in any story template and prompted users to provide an arbitrary number of fill-in-the-blanks. You can see the working code for that here. I was particularly pleased to discover the syntax for list comprehensions and how to unpack/spread a list.

However! If you look at that code, you’ll notice that the templates rely entirely on order to figure out which user responses go where, which also means they can’t reuse answers. For instance, if I want a story like “There was a pirate named X. X said ‘ahoy!’” I would have to prompt the user for X twice.

So I went off in search of the Racket equivalent of Python’s f-strings. Someone suggested at-expressions, but AFAICT with those the values need to be interpolated at compile time while for this project the values are received from the user at runtime. You can see my totally ineffectual effort to deal with this problem here.

Towards the end of the day Recurser Tristan offered to pair and we worked out a simpler solution that didn’t involve any fancy namespacing or syntax definition or calls to eval. You can find it here; it’s a little verbose but it does what I want in a relatively straightforward way.

I have mixed feelings about this. On the one hand I fully believe it’s best to do things as simply as possible. On the other hand this is a learning exercise meant to help me understand Lisp/Racket’s macro system. Should I do yet another iteration that makes use of macros somehow, for the sake of using macros? How do I know that this problem even could make use of macros?

I think my plan tomorrow will be to start on the code version of Madlibs (Codelibs!!), but we’ll see.