Details, details

While working on Hecl, it has sometimes really surprised me how much it’s possible to delve into seemingly trivial details. Just for starters, we’ve been talking about what to call string commands.

Originally, I’d used names like slen and sindex to have something quick and easy to write, but no one seemed to like them that much. Wolfgang implemented a bunch of new string commands (very much appreciated!), and took the Tcl approach of command subcommand, like string index, and also suggested some other things like str.index. I am strongly opposed to the string length style commands though, because I think that’s just too much typing for a language that’s supposed to get you up and running quickly. The compromise I proposed and implemented was to use more ‘C style’ commands like strlen, strindex, strfind, and so on. I’m not a big PHP fan, but they seem to have taken a similar approach. It’s not too verbose, but you can tell more or less what the command is for.

Even more seemingly trivial – indexes for strings and arrays. Hecl takes the Lisp approach rather than the C approach, and utilizes commands instead of syntax, even for list and string access, so to get the second character of the string “foo”, you would do: strindex "foo" 1 . Like C, Hecl indexes start at 0. Where things get tricky is in calculating indexes “from the end” – how do you tell the language to “fetch the last character from the string ‘foo'”?

Tcl lets you write a string like “end” or “end-1”, which is handy compared to the process you would go through in a language that doesn’t help you out: take the length of the string, and subtract from that as needs be. I prefer the approach that I first saw in Python, though, use -1 for the last character:

>>> "abcde"[-1]

Ruby takes the same approach. It’s nice to be able to just insert a -1 rather than have “end-1”, which sort of looks like an expression, but isn’t really (Hecl doesn’t even have normal infix expressions), especially because you can easily calculate the -1 and insert it as is, instead of having to do some string interpolation like "end-$foo".

Where things get tricky is when you want to do something like specify a range, say from the 3rd letter to the end of the string.

In Python, they use syntax, specifically by simply not including the end of the range:

>>> "abcde"[3:]

That doesn’t work really well with Hecl – I suppose we could pass in a blank string instead of a number, like so:

strrange "abcde" 3 ""

But that’s not very clear, and IMO it’s slighly ugly to pass in a string where you want to use a number. I had a look at Ruby’s behavior, and found it more to my liking:

irb(main):001:0> "abcde"[3..-1]
=> "de"

The difference with Python is that the second index is inclusive, whereas Python includes the character at the first index, and excludes the second:

>>> "abcde"[3:-1]

I decided the Ruby approach worked best for Hecl, so now we have:

hecl> strrange abcde 3 -1

If you’re still reading this, I guess you see what I mean by how much one must delve into what must seem like very unimportant minutiae to… well… normal people!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s