Thoughts on Perl and Emacs, technology and writing

Looping Syntax in Various Languages

Okay, this post is going to be quite long. I’m going to start with a basic problem I was solving in emacs lisp. From there I’ll segue into thinking about looping syntax and finally I’ll do a bit of benchmarking as I’ve got the code already and people seem to like that (the scheme, ocaml, c++ speed comparison is by far the most popular post on this blog followed by this).

Futzing around with Project Euler is something I do for fun. Most recently I was looking at problem 73 – count the reduced proper fractions with a denominator less than or equal to 10,000 between 1/3 and 1/2.

Emacs Lisp Solution

Emacs Lisp is usually my default language for doing this kind of thing as I’m already in my text editor and there is a REPL to experiment with.

First of all, it is clear that I’m going to need a function to calculate the greatest common divisor. I found an imperative Pascal implementation of Euclid’s algorithm here. A brief aside – I searched for Pascal deliberately as I generally find it very clear. Does anyone else do that?

Thinking ahead, I’ll probably know what the gcd is before we call make-fraction as only fractions with a gcd of 1 will be actually counted amongst the solutions. I’ve therefore made gcd an optional parameter as a nod to efficiency.

The most annoying thing is the primitive looping constructs. while is the basic and obvious built-in. It also has a slew of macros beginning with doXXX including dotimes and dolist not to mention the mighty common lisp loop macro.

I don’t know loop (but I’m going to learn it), but after messing about with do* for a few minutes, I realised it wasn’t the looping construct for me.

Now, the great thing about lisp is supposed to be that if you don’t like the syntax you can add your own with macros. Unfortunately, I haven’t got around to that yet as a bunch of people have already designed most of the syntax I like.

Scheme Solution

When I read some of the earlier posts on this blog, it seems that scheme has got some nice generator syntax (aka eager comprehensions) for handling nested loops.

So, Conclusions

For this particular task (looping and integer math) Emacs Lisp is slow, but not that slow compared with another scripting language. I really like the scheme looping constructs and mzscheme is surprisingly quick (again, just for this tiny thing), not too far from the ocaml – although again I should emphasise that the ocaml is a terrible hack. And finally, I need to learn how to use loop properly.

@trimtab – iterate looks nice, but I’m looking for a looping mechanism to improve my emacs coding. I just thought I’d do a quick comparison with looping in other languages while I was messing around.

@Porges – thanks for that – it looks similar in spirit (if a bit different in syntax) to the scheme. How long does it take to run on your machine?

@Jisang – sit-for does redisplay as the manual says. If I don’t add it then the screen does not redraw until the loop has finished executing, which took around 5 minutes in my first effort. I added the sit-for so I could check on the progress.

Compiling your solve-it function with compile-defun sped up Emacs-Lisp for me a lot. The uncompiled version of solve-it completed in 56 seconds for me. After compiling it, the time shrank to 9 seconds. That is closer to your expected MzScheme time and I believe that MzScheme will byte-compile things behind the scenes for you.

I don’t understand why you are comparing emacs lisp to scheme. I would not have considered emacs lisp a fast implementation of lisp. Wouldn’t it make more sense to compare sbcl (or clisp or clozure or even Clojure) with scheme?or emacs lisp with a common lisp implementation?

1. I implemented my solution in emacs lisp using (while …) and (setq …)
I didn’t like it so I had a look at looping in other languages.

2. I liked the look of eager comprehensions so I tried those and I’m possibly interested in learning Ocaml so I tried that. I added the Perl as a reference.

3. I added the benchmarks as I thought other people might be interested (and it looks like I was right – it has had the third most number of visitors in the last 30 days and its only been 2 days). I don’t really care that much.

I guess what came across most strongly was the benchmarking, but what I was most interested in was the various syntaxes for looping.