Skip to content

Lua Programming Gems out!

Hot from the presses, the book is finally here. The table of contents, the front matter and the free chapter 2 are in the book homepage.

Will code for money

Thanks to the Financial World Crisis, my employer wants to relocate me to a place where it will spend less money paying me and with less infra-structure. As I do not intend to relocate, I therefore offer my good coding skills for money. Remember that Brazillian paying rates are lower than European and American ones! :-) The link to my resume is at the side.

Equivalent of and-let*?

By this point I believe it’s clear that I am a big fan of Scheme. Lately I have seen a sharp rise of interest in Ruby in some virtual places I attend (#lisp-br, twitter, IM with coworkers etc.). I am sure Ruby is a fine language, even in spite of several complaints I hear from some of its users. After all, it has symbols, metaprogramming, blocks (a sort of closures), lambdas and more! I’d really take the time to learn it, if it wasn’t for Matz himself:

Some may say Ruby is a bad rip-off of Lisp or Smalltalk, and I admit that. But it is nicer to ordinary people.

Now that is a demotivator. But Ruby must be evolving, and that quote may be outdated. So I have thought of a question to the Ruby-lovers, that may help me make up my mind to take the time to learn it. One of the best Scheme macros is, in my opinion, and-let*, from the SRFI-2. How would one write the following in Ruby, a procedure that I wrote a couple of nights ago?

;; Tries to get session from current request
(define (get-session request)
  (and-let* ((cookies (request-cookies request))
             (p (assoc "session_id" cookies))
             (sid-str (cdr p))
             (sid (string->number sid-str))
             ((integer? sid))
             ((exact? sid))
             (sp (assq sid *sessions*)))
    (cdr sp)))

In the and-let* macro, the clauses are evaluated and the variables are bound sequentially, creating new scopes for the next expressions. If any of the forms evaluates to #f (false in Scheme), the computation is aborted, no other expressions are evaluated, and the whole form evaluates to #f. How would I write this in concise Ruby?

Paul Graham’s strategy

I have disappeared for a while, but my excuse is that I have been coding furiously. I intend to write a lot more in the not-so-far-away future. In the meanwhile, a quick post.

From time to time I read another Paul Graham essay, usually when they are cited in some Reddit or Slashdot thread. He really writes well, and although I do not agree with everything he says, I must say I agree with most of it. “Beating the averages” was very influential on some important decisions I have made, and for a lot of other people as well.

Today I found an essay called “Six Principles for Making New Things“, in which he describes his modus operandi:

Here it is: I like to find (a) simple solutions (b) to overlooked problems (c) that actually need to be solved, and (d) deliver them as informally as possible, (e) starting with a very crude version 1, then (f) iterating rapidly.

I agree 100% with him here. There are a lot of overlooked problems out there, in search of solutions, that just seem “simple” enough. But they need to be solved and, when someone does it, everybody else wish they had thought of it. But they were there, all the time. Moreover, the initial solution does not need to be perfect, as long as it solves an immediate need well enough and the solution can be iterated rapidly, which is a very, very important point. When someone creates a market, there is soon afterwards a rush of new competitors. Coming up with a simple solution which does not evolve quickly is the recipe to lose to another faster, more dynamic, more focused team.

I already had something like Graham’s Six Principles in the back of my mind for a while. It was good to see them written by someone who has made millions of dollars selling (Lisp!) software.

Lisp in Latinoware 2008

There will be presentations about Lisp in the V Latin America Conference on Free Software, via call-with-hopeless-continuation.

Meroon for Gambit-C

Meroon is a Scheme object system by Christian Queinnec. It is portable, fast, CLOS-like and reflexive. The Meroon home page cites versions for several popular Scheme systems, including Gambit-C. But the links to the Gambit-C version are broken. Doing a bit of research, that actually involved some guessing, I found the Meroon Gambit-C port in Bradley Lucier’s home page. Instructions to build this Meroon port can be found in this thread in the Gambit-C mailing list.

I am not a heavy user of object orientation, but I can certainly see its value where it is worth. In the LiSP book, for instance, a beautiful interpreter and a compiler are written in OO-style, in chapter 3 and 10, respectively. Another example is writing a scene graph for a Computer Graphics application, it almost begs to be written object-oriented.

The need for speed

I have found a very interesting thread in the Gambit-C Scheme mailing list. Marc Feeley tries to dismiss the myth that floating-point calculations are inherently faster in C/C++ than in Scheme, which is taken as common wisdom. The the work of Bradley Lucier is cited as a case of very fast numerical Scheme code.

Bradley himself replies, in which he explains that his code is roughly half as fast as it would have been if coded in C, but the memory bandwidth is the culprit and it does not matter anyway. An interesting part of the email is this:

I didn’t mind rewriting the level-1 BLAS code in Scheme, however, as it didn’t seem worth the trouble to get a small speedup in the entire system just to use a dot-product or saxpy written in C and called from an FFI. I have enough difficulty in getting students to admit to themselves that, yes, this system is fast (just about as fast as any expert could have written it, and probably much faster than your average graduate student could have written it) and it’s flexible (it’s only at the end of the course that some students reluctantly admit that they could not have finished their semester project in their favorite language, whether C or C++), and that the speed doesn’t arise from level-1 BLAS written in C (because they’re written in Scheme). One point that most students seem to take away from the class is that multigrid is one hell of a lot faster than conjugate gradient, and many of them have been using conjugate gradient in their own projects simply because it’s too hard to program multigrid in C or C++ (at least the first time you try it). So one gets a lot of speedup simply by being able to program more sophisticated algorithms.

There is also a toy ray-tracer written in Gambit-C, Schemeray, which was further optimised by Marc Feeley, and even more by (again) Bradley Lucier. The times are impressive, thanks to all the clever compiler tricks Gambit-C uses when generating C code, like inlining, partial code evaluation and generating one C function per Scheme module (avoiding expensive inter-module C calls).

The code generated by Gambit-C is fast enough for 99.99% of the applications out there. And this is using a strong, dynamically-typed language, with first-class closures and continuations, arbitrary-precision numbers, and the powerful macros of the Lisp family of languages. There is hardly need to use C or C++ even in the most demanding applications. The same cannot be said of Perl, Python or Ruby, whose applications usually run much slower than one created with Gambit-C. And, if the need really arises, interfacing Gambit-C with C code is extremely easy.

Call me a whiner

But there are out there things like this:

We don’t really care about the title but we want someone good (who doesn’t?). We need someone able to model and code well (No architect who never codes). The ability to communicate well with a team is also a big plus. You don’t need to speak French (we are in France) but a reasonably good English is mandatory.

    If You:

  • like coding 4+ hours straight
  • like to solve a coding problem elegantly (and are bothered if can’t)
  • like and read real CS books (SICP, EGB, TAOCP, etc…)
  • code in Haskell, Python and C++
  • are a gamer (this one is optional)
  • are interested in computer graphics (optional too)
    What we offer:

  • interesting problems and creative freedom
  • quality of life (no overtime, sunny countryside, French food and low rent)
  • a pay in Euros
  • comfortable workplace, etc…
  • coding in Haskell, Python

The boss really codes. He likes Haskell, Python, Reddit, and wants the team to be there for the long run (i.e. happy). He also modestly wrote and posted this jobs offer.

So let me see: Haskell, games, and Computer Graphics. How sweet. Also, note the emphasis on how the boss and “architects” still code. In underdeveloped places, coding is just for drones, and bosses and “architects” can’t do it to save their lives.

Not luck

There is a recurrent angst I feel every time I think I could be working daily in a decent programming language. Usually the management people believe the choice of programming language is irrelevant, but we developers know better. Languages shape our thoughts.

Today I found an interesting post about getting work using non-mainstream languages.

Well, unlike Java jobs or C# jobs or VB jobs, where one can scan the newspaper or go to dice.com, these magical jobs are not the ones I get when recruiters solicit me (three times a day). No, they appear between the cracks of the sky when it rends in two. All but one contract (that did come from dice.com) came from the company CEO pulling me aside or phoning me out of the blue from the materials I had published inside the company where I was employed, or from my web-sites on programming languages.

This basically means you must be a reference. You must blog a lot or, better yet, write even a book. When your name is said in a company meeting, somebody of the team must already have heard of you. Needless to say, this is not easy at all.

Usually and because of the exclusivity of the work it takes anywhere from at least 3 months to 2 years to secure these kinds of contracts, so the adage “Don’t quit your day job” is an appropriate one here. When I do get these contracts, however, they usually last longer (3 years) than the Java/C++/XML/web-services-code-grinder ones (that usually last around 6 months), and the peer group is much more intelligent, genteel and just plain more interesting than the code-grinding crowd. Is is harder to find these magical contracts? Yes, they are rarified air. But are they worth preparing for and then finding? Definitely.

Here we see that even after all this work, it is still very hard to find these dream jobs. You can wait as long as two years to get one, simply because in most companies the management employ Java/C#/VB drones anyway, as they are cheaper and easier to find. Only when they face really hard problems they start thinking of alternatives.

This post only reinforces what was already in the back of my mind for a while: The least hard way to earn money and be happy at the same time is to start an own business. Now if I could only move my lazy ass…

Misuse of technology, XML case

I do not have strong feelings about XML, be it for or against it. I don’t use it much because I use either S-Expressions with Scheme or Lua tables with Lua. I believe XML can be helpful, but people need to know how to use it.

I have come across an application of XML to store an animation, frame by frame, like the animated GIFs of old. Each frame is then an image, stored as an XML element. The bizarre part is that every pixel is stored as an individual element inside the frame:

<frame id="Frame001" width="180" height="240">
  <array type="Int8">
    <Int8 value="230"/>
    <Int8 value="012"/>
    .
    .
    .
  </array>
</frame>

I believe this is the most “by the book” implementation. But this is far from optimal. When inspecting the files with a text editor (yes, I needed to do that) it was very painful. Besides, the files are huge! Why not be sensible and use Base64 encoding for the image part:

<frame id="Frame001" width="180" height="240">
  TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz
  IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg
  dGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu
  dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo
  .
  .
  .
</frame>

Nobody could inspect the information of previous format anyway, the files gets much smaller, one could edit them by hand (like I did to copy frames between animations). And no, the files are not being compressed because they are part of a build process.

It only remains to be seen why it is done this way. I believe it was a mixture of a non-too-intimate knowledge of the technology and a desire to use it in it “purest” form, like those people who must design their programs with all the design patterns known to man. With time they may learn that a sensible dose of pragmatism is very healthy.