An interactive version of this article is available here: http://wordsandbuttons.online/learn_you_a_lisp_in_0_minutes.html
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.
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.
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.
1 What number would this evaluate to?
(+ 2 2)
2 Is this
(= (+ 2 2) (* 2 2))
3 Is it
(first (list ‘a ‘b ‘c))
4 Would it print
(define apples 5)
(define oranges 6)
(if (< apples oranges)
5 What number would it be?
(define (dbl x)
(* 2 x))
6 What number will this result to?
(define (fact x)
(if (< x 1)
(* x (fact (- x 1)))))
7 What would this function do to the list?
(define (qs xs)
(if (empty? xs)
(middle (first xs))
(others (rest xs)))
(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.
- 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
- Apples is just a variable set to 5, and oranges set to 6.
5<6so it should print
dblis defined to double the argument. Then it’s called with
2as an argument, so the answer is
factreturns 1 for anything less than 1 and an argument
fact(0) = 1,
fact(1) = 1*1,
fact(2) = 2*1*1and
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 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.
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.
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.