Recursion

Recursion is another of those concepts that seems to pup up everywhere you look, once you understand it. I don’t just mean it in a programmatic sense, but it’s easier to get it if you’ve coded for a while.

Thinking of thinks in terms of themselves is a big plus, conceptually. It simplifies things when you treat small parts of a system as if they were the whole system, with similar parts of their own, and so on. You start beginning to see patterns you hadn’t thought existed.

OK, that last paragraph was a very vague. I don’t think I can do a better job explaining what I mean though: at least not in a non-programmatic way. So here follows some code-related stuff.

I’m learning a little Lisp these days, when I get the time. Am also trying to build a small app for myself, to keep track of my expenses and so on. I don’t know Lisp, and although there are probably hundreds of related libraries I could use to code my app a bit faster, I intend to try and build stuff on my own as I go along. Which probably means I’ll keep getting side-tracked with such interesting side-projects, and so never finish the actual app. Won’t be the first time that’s happened.

Anyway, I wanted a way to enter dates in plain English, and have the app parse ’em properly. So I went about building a customer date parser. Something that will recognize inputs like

1st April 2007, 1/4, 1 April, 1-4, 1-4-2006, 1.4.7, etc

Something that’s as flexible as possible, of course. I don’t want to remember a list of valid formats, I just want to enter a date. To go about doing this, I need to split a string up into some tokens. Notice that I’m sneakily introducing technical-sounding stuff at this point: it’s so that I seem knowledgeable.

OK, split is a very basic function provided by most languages. I couldn’t find one built-in for Common Lisp, so I went about building one.

I even made a mental spec: the function should take an input string, and an input list of separators, and return a list of tokens I get by splitting the input.

I wanted it to handle multiple separators, that is:

(split "1-April 2007") gives me ("1" "April" "2007")

How’s this for an implementation? In pseudo-code:

function splitter (input-string list-of-delimiters):
  if input-string is empty:
    return nothing
  else:
    find the first position any of the delimiters occur in the input-string
    return a list that has:
      the first part of the input-string, that is: 
        the portion from it’s start to the first occurrence of a delimiter
    and: 
      the result of splitting the rest of the string

I’m not posting the actual Lisp code I wrote for this, as it is still somewhat ugly. I’ve just started learning it after all.

The point of writing all this? I forget: have got some other interesting function I want to code that keep distracting me.

Comments (2)

  1. Zach Beane wrote::

    There's a handy library called SPLIT-SEQUENCE that does what you wrote. It may give you some handy implementation ideas.

    Monday, May 7, 2007 at 18:51 #
  2. Ankit Solanki wrote::

    Thanks. I just remembered seeing that package in the SLIME demo

    Monday, May 7, 2007 at 21:17 #