Tag Archives: Lisp

Uncategorized

Continuations

By training and experience, I’m largely an object-oriented programmer. I was a freshman comp. sci. student when the Java wave hit my university, so that’s what I was fed. I didn’t learn anything else but Java until the summer after my sophomore year; then, I was involved in a summer research program for which I used C++. Between then and the end of my undergraduate tenure, I had picked up more C++ and learned C (I should add here that my department was and is a very good c.s. department – they just didn’t use functional languages as teaching tools).

Where did that leave me? I knew how to write nice object hierarchies, tight structural/procedural code. However, Boost wasn’t a big deal yet, and I had only barely dabbled in anything else, so I was missing a huge piece of the puzzle: functional programming. I used lisp for a couple of programming assignments in graduate school because I wanted to learn the language – and I did learn the basics, but what I didn’t learn was important functional concepts.

Now, as a working professional programmer, I’m still trying to learn functional concepts. Some are pretty easy: immutability and recursive computation. These are very basic – even recursive functions were taught in Java programming classes.

But the most interesting element of functional programming is higher order functions, which are the basis for some very powerful forms of abstraction. This is what I was really missing in my repertoire.

So now, the point: I’ve been trying to learn a web framework called Weblocks. It’s freaking cool – but it makes heavy use of continuations for control flow (one of its major features). I had never learned about continuations before, so this was (and still is, to some degree) making it hard to learn the framework. So I was reading about continuations online (here ,here, and here) and came across a reference to Paul Graham’s book, On Lisp. I have a copy that I printed for myself, so I looked up the chapter on Continuations to gain some insight, and found the best explanation I’ve seen so far:

A continuation is a function representing the future of a computation. Whenever an expression is evaluated, something is waiting for the value it will return. For example, in

(/ (- x 1) 2)

when (- x 1) is evaluated, the outer / expression is waiting for the value, and something else is waiting for its value, and so on and so on, all the way back to the toplevel…

We can think of the continuation at any given time as a function of one argument. If the previous expression were typed into the toplevel, then when the subexpression (- x 1) was evaluated, the continuation would be:

(lambda (val) (/ val 2))

That is, the remainder of the computation could be duplicated by calling this function on the return value.

I found the little example shown here to be the best way try to explain continuations. Trying to understand the applications of continuations is a beast, and without this basic understanding it’s very easy to get lost.

Now…back to Weblocks…

Uncategorized

Lisp: no easy download

Over at LispCast, eric’s got a new post up called No easy download. In it he talks about the difficulty of being new to Lisp, wanting to learn, and not knowing where to start.

He’s right. It’s way too hard – and I’d argue it’s one of the primary factors that hampers Lisp adoption. As one of his commenters points out

Perl, Python, and Ruby have the advantage that they are the only or at least canonical implementation of that particular language. Therefore, you go to perl.com or python.org and there it is.

When I run lisp, I don’t type ‘lisp.’ My implementation isn’t at lisp.org or anything like that because there are a bunch of implementations, none of them strictly canonical. If you’d gone to sbcl.org or cons.org (CMUCL) for instance, you would have found an accessible download link, although not really a mascot.

The comment is correct that there isn’t a canonical implementation of Lisp. But why should it matter? There can be a canonical place to go to for Lisp resources. There’s common-lisp.net, which isn’t bad, but as eric points out, it doesn’t lend itself to the “getting started” meme as well as python.org and ruby.org do. What about lisp.com? Well, it redirects to yeah.com, which has nothing to do with Lisp that I can tell. I can’t be sure though, because my employer blocks it:

\"Lisp.com redirected to yeah.com\"

Alright then, what about lisp.org?

Ok, it’s there and it’s Lisp-related, but it’s mostly about some group called the Association of Lisp Users. What about Lisp? Where can I download it?

Directory index of http://lisp.org

I can learn about the history of this group, read about their board, their members, their meetings, and their sponsors. A little further down, I can read about past conferences. Further down, I can read about Lisp resources.

If the ruby.org page had been this way, there might not have ever been a Ruby on Rails, and there’d be no Basecamp or Twitter (ok, there would be, but they’d be made from PHP).

Now, to be fair, the ALU has a CL Gardeners project that is aimed specifically at dealing with this issue, and the ALU Wiki is actually pretty good – but the wiki should be the index of lisp.org, and that page should have a link to the alu.org homepage.

Of course, eric’s post spurred some discussion over at News.YC, in which I participated. Said radu_floricica:

Arguing between 3 or 4 clicks doesn’t change anything. There are a lot of problems with beginners and lisp, but they’re far from how to get to the first implementation. As a newbee myself, googling for lisp environments was damn easy and instructive: i knew within 20 minutes that I should use linux, and if i really wanted windows (as I did at first) there was only one choice. When I finally had access to a decent linux it took me maybe another 20 to confirm that sbcl is the one I wanted. All that gave me lots of background information on the side.

Now the first brush with asdf on the other hand was a nightmare. I still don’t understand why I have to become an expert in pgp (definitely not just a beginner) just to use asdf-install.

I think the main problem for beginners is the effort it takes to install a reasonable environment. I tried two versions for a web application server: ucw and weblocks. Weblocks meant installing more then 10 different packages and source code is pretty much standard documentation – but it’s ok because it’s understandable and officially beta anyways. UCW is the standard – after a year and a half I still couldn’t produce an installation running on a port different then 8080 (I use 8080 for tomcat for all my machines).

All this effort is way disproportionate to the effects. Installing emacs and slime is may not be a breeze, but once you discover that googling “emacs configuration” brings up a bonanza it’s worth it. (Also that .emacs in windows is _emacs… that’s half a day I’ll never have back). But so much effort just to get a server running… if you want to help beginners write a better ucw tutorial, don’t just rearrange the links on the frontpage of lisp sites.

And my response:

Arguing between 3 or 4 clicks doesn’t change anything.

You’re wrong. And besides, the argument isn’t between 3 or 4 clicks. It should be no more than 2, and preferably one.

There are a lot of problems with beginners and lisp, but
they’re far from how to get to the first implementation.
As a newbee myself, googling for lisp environments was
damn easy and instructive: i knew within 20 minutes that
I should use linux, and if i really wanted windows (as I
did at first) there was only one choice.

You’re still wrong. Not every newbie even knows what to google for, and even if they did, it shouldn’t take them 20 minutes of web searching to figure out what they should download. And even that didn’t lead you to the right place – there is more than one option for Windows (cusp, allegro, lispworks, and clisp for starters – that’s not even counting scheme stuff), Linux isn’t the only way, and SBCL isn’t the only thing going on Linux. But a newbie either

a) has no way to figure all that out in a reasonably short amount of time b) isn’t going to try

And beyond that, you’re still missing the point: he shouldn’t have to.

Once you’re concerned with learning emacs and slime and customizing your environment and installing weblocks and getting asdf and asdf-install to work, you’re already committed. Difficulty doesn’t matter nearly as much by that point. Being able to easily download and dick around in the REPL is the number one hurdle that keeps people from trying Lisp.

The rest of the post I agree with, and I don’t argue that there are hurdles beyond the first one that are bigger and harder to conquer, but that’s irrelevant if newbies aren’t willing to jump the first one.

Uncategorized

Picking up a Lisp

I’ve recently gone back to learning Lisp. I used it a little bit in graduate school to do some homework assignments in one of my algorithms class and learned some of the basics there, but since I started my professional career, I haven’t gone back to it at all until now. I got Paul Graham’s book On Lisp and have made my way through several chapters, and it’s quite enjoyable.

One thing I keep noticing is how similar Lisp and C++ are. Many Lispers, of course, would regard this as heresy, but to me it makes perfect sense. For example, take this passage from the book:

Many languages offer some form of macro, but Lisp macros are singularly powerful. When a file of Lisp is compiled, a parser reads the source code and sends its output to the compiler. Here’s the stroke of genius: the output of the parser consists of lists of Lisp objects. With macros, we can manipulate the program while it’s in this intermediate form between parser and compiler. If necessary, these manipulations can be very extensive.

Of course, the two languages aren’t exactly the same in this regard. C++ templates can’t be manipulated in the same way first-class C++ expressions can be; they are powerful, but really only give you full control over the type system. Being able to say lots of things about types in C++ will get you a long way, but not as far as Lisp macros will get you. Still, the similarities I think are striking, especially considering some of the newer techniques that have been discovered with C++ templates that let you go a bit beyond just the type system by doing some really obscure things with types.

A number of other similarities I think are significant; Lisp and C++ are the two languages with the best multi-paradigm support to which I’ve been exposed. Lisp tends to tilt functional, while C++ tilts procedural, but they both support all the major programming paradigms to practical degrees (although, to be fair, it would be easier to write an entirely procedural and/or object-oriented program in Lisp than it would be to write and entirely functional program in C++).

Anyway, just blurting some thoughts out loud while reading On Lisp.

Uncategorized

First Things in my .emacs

Pardon the extra linebreaks. I don’t normally format my lisp code this way, but the layout of my blog makes this snippet linewrap and hard to read.

(global-font-lock-mode t)

(defvar autosave-dir "~/.emacs.d/autosave")
(make-directory autosave-dir t)

(defun auto-save-file-name-p (filename)
  (string-match "^#.*#$"
                     (file-name-nondirectory filename)))

(defun make-auto-save-file-name ()
  (concat autosave-dir
          (if buffer-file-name
              (concat "#"
                         (file-name-nondirectory buffer-file-name)
                         "#")
            (expand-file-name (concat "#%"
                                                 (buffer-name)
                                                 "#")))))

(defvar backup-dir "~/.emacs.d/backup")
(make-directory backup-dir t)
(setq backup-directory-alist (list (cons "." backup-dir)))