Learn you a Lisp in 0 minutes

7,908 reads

An interactive version of this article is available here: http://wordsandbuttons.online/learn_you_a_lisp_in_0_minutes.html

But why?

Learning a language you are not going to write in professionally is like visiting a country you are not going to move in to. It may be tiring, but it’s fun, educational and it makes you appreciate other cultures. And Lisp is particularly fascinating to learn because of its influence on modern programming. You might see traces of Lisp in the most unexpected technologies like WebAssembly or GCC internal representation.

The only reason not to learn Lisp, or any other language, is the amount of effort it usually takes. But! It if you only want to know the very basics of Lisp, you wouldn’t have to spend any effort at all.

But how?

If you can read this article, then you should know English. And this also means that you know a bit of French as well. Words like “concept”, “culture”, “action”, “instinct”, “machine”, “science”, and many more are actually shared between two languages. Words like this are called “cognates”, and the word “cognate” is almost a cognate itself.

This happens because of long lasting French influence on English language. And the same works with Lisp too. Its ideas and concepts are so widespread among modern languages then, if you have any substantial experience in programming, you automatically know some Lisp.

So?

I’ve made this test so you could prove yourself you know Lisp. As many languages and dialects belong to Lisp family I should specify that this test is based on WeScheme.

The test starts from very simple things and gradually progresses into obscurity. It’s ok not to get all the answers right, some of them would only work for programmers with functional programming background.

Amuse-toi!

1 What number would this evaluate to?

(+ 2 2)

2 Is this true or false ?

(= (+ 2 2) (* 2 2))

3 Is it ‘a , ‘b or ‘c ?

(first (list ‘a ‘b ‘c))

4 Would it print Apples! or Oranges! ?

(define apples 5)

(define oranges 6)

(if (< apples oranges)

(printf "Apples!")

(printf "Oranges!"))

5 What number would it be?

(define (dbl x)

(* 2 x))

(dbl 2)

6 What number will this result to?

(define (fact x)

(if (< x 1)

1

(* x (fact (- x 1)))))

(fact 3)

7 What would this function do to the list?

(define (qs xs)

(if (empty? xs)

(list )

(let (

(middle (first xs))

(others (rest xs)))

(let (

(left (filter (lambda (x) (<= x middle)) others))

(right (filter (lambda (x) (> x middle)) others)))

(append (qs left) (cons middle (qs right)))))))

(qs (list 4 5 6 1 2 3))

That’s it. The answers are under the baguettes.

By jules / stonesoup (bocadillos-3) [CC BY 2.0 (http://creativecommons.org/licenses/by/2.0)], via Wikimedia Commons. Well, I just needed something long to screen the answers, so why not baguettes?

That’s just prefix notation for 2+2 , so the answer is 4. It is true. 2+2 = 2*2 . First element of list (a, b, c) is a . Apples is just a variable set to 5, and oranges set to 6. 5<6 so it should print Apples! . Function dbl is defined to double the argument. Then it’s called with 2 as an argument, so the answer is 4 . Function fact returns 1 for anything less than 1 and an argument x multiplied to fact(x-1) . So, fact(0) = 1 , fact(1) = 1*1 , fact(2) = 2*1*1 and fact(3) = 3*2*1*1 = 6. It is a factorial of 3 also known as "3!". This is a simple quicksort implementation. The list gets split into pieces: some middle element, the “left” list with all the other elements lower than or equal to middle, and the “right” list with all the other elements greater than middle. “Left” and “right” get quicksorted recursively until they’re done and the function then returns sorted left + middle + sorted right . So the list (4, 5, 6, 1, 2, 3) becomes sorted: (1, 2, 3, 4, 5, 6) .

If you have read this far, congratulations! You now know you know Lisp!

If you got five answers right, you would probably be able to write simple Gimp plugin on configure Emacs.

If you got all the answers then you have a knack for functional programming. If you haven’t been working in functional language before, perhaps you should consider trying one.

Now what?

Of course this test covers the very basics of Lisp. It doesn’t even touch the meta-programming, which is arguably the mightiest Lisp feature. But learning a bit of Lisp is not the whole point. The test actually shows that you should not stop at this, but learn more are more languages that you are not going to use professionally.

Consider this. If practicing with JavaScript, or Python, or C#, or whatever your primary language is made you unknowingly learn some Lisp, then shouldn’t it work the other way around as well?

Eric Raymond once wrote:

“Lisp is worth learning for the profound enlightenment experience you will have when you finally get it; that experience will make you a better programmer for the rest of your days, even if you never actually use Lisp itself a lot.”

But I don’t think Lisp is in any way magical. I think that learning any language enriches your experience. The more it’s different from what you do every day the better. The more you know — the more ideas you have when approaching any new task. That’s the whole point of linguistic tourism.

If learning new languages, new concepts, and ideas makes you a better programmer, then it is not just a waste of time, but an investment in your professional career.

N’est-ce pas?

Tags