Lisp for Progreginners

Progreginners? Well, that’s a term I just coined up: programmers + beginners*.

What is Lisp?

[…] Originally specified in 1958, Lisp is the second-oldest high-level programming language in widespread use today. Only Fortran is older, by one year.

[…]

As one of the earliest programming languages, Lisp pioneered many ideas in computer science, including tree data structuresautomatic storage managementdynamic typingconditionalshigher-order functionsrecursion, the self-hosting compiler, and the read–eval–print loop.

The name LISP derives from “LISt Processor”. Linked lists are one of Lisp’s major data structures, and Lisp source code is made of lists. Thus, Lisp programs can manipulate source code as a data structure, giving rise to the macro systems that allow programmers to create new syntax or new domain-specific languages embedded in Lisp.

– that’s Wikipedia

Let’s begin.

Specified in 1958? This is 2018 – languages improve every decade. Frameworks change thrice every decade (most, anyways!). And you’re telling me about such an ancient language?

Here’s the deal: lisp is still in widespread use today! Yes, no where near as active as python, C++, Java; but it is still used.

Why do you think would a language survive that long, without any merits?

  • Garbage collection is good for prototyping. Lisp was the first to use it.
  • Dynamic typing, again, is good for prototyping. Lisp had it already.
  • Higher order functions. Lisp has it.
  • Tail-recursion optimization. Lisp.
  • REPL. Lisp.
  • Meta-programming. What?

The point? Lisp has had features that let it survive. Other languages are merely adopting the lisp features! And perhaps, there still is one feature not present in other languages: meta-programming.

So what?

Lisp is powerful! If you have something in mind, about which you are not sure whether language constructs exist, lisp is the way to go.

Now, very very few applications require the power of lisp. It could even be that not everyone can handle the power of lisp. So, for all practical purposes, python could be your friend, in today’s day. So, what is this power?

Lisp, natively, does have a if-then-else statement:

(if condition then else)

Here’s an example:

(if (evenp x) 'even 'odd)

This means: if x is even, return ‘even’, else return ‘odd’.

It does not have a switch-case statement natively. However, it does have macros! And they enable us to “extend” the language:

(cond ((primep x) 'prime)
      ((evenp x) 'even)
      ((oddp x) 'odd))

So that you can write the above. And the “macro” ‘cond’ will “translate” it into

(IF (PRIMEP X)
    'PRIME
    (IF (EVENP X)
        'EVEN
        (IF (ODDP X)
            'ODD
            NIL)))

What does that code mean? If x is prime, return ‘prime, else: if x is even, return ‘even, else if x is odd, return ‘odd.

Which of the two is more clean if for you to decide.

This is meta-programming!

Why isn’t lisp most widely used then?

One. When lisp was first out, it was slow, very slow.

Two. Think about it. If every one used macros, how would developers understand each other’s codes, quickly? Macros are great if you want to work alone, but in a team, in a large team, every team might end up developing specialized language constructs! This question on stackexchange summarizes the situation well.

Three. Due to this loop, there are much fewer libraries in lisp. And fewer libraries means fewer new users.

Today, python comes close. But my personal hate for python is its reliance on tabs/spaces instead of parenthesis. It makes it too deep for my tastes! (In contrast, in lisp or C++, I’m free to decide the amount of white space in my code!) For machine learning, python is likely the way to go. However, if you’re to access the past centuries’ research on AI (as also expand your mind with metaprogramming), learning lisp might be useful. Even then, efforts are underway to convert lisp code to easier languages like python.

Further, lisp is very simple – down to the parsing!

Homework and Follow-up

I said this article was aimed at beginners. However, I couldn’t resist adding the following terms. These can be looked up fairly easily:

  • Garbage collection
  • Dynamic typing
  • Higher order functions and lambdas
  • Recursion and tail recursion, and their relation with loops
  • Read-eval-print-loop
  • Why Clojure? (see PPS)
  • More on Hy (see PPS)

PS

PPS

Handling the ‘lack of libraries’ problem, Hylang is a dialect of lisp implemented over python. What this means is that it has access to python’s libraries! Clojure is another dialect of lisp implemented over java.

* There are tons of articles out there, which explain ‘Why Lisp?’. But I did not find any that explained it in beginner’s terms. Now, note that with just 2-3 smallish projects under my belt, I, too, am just beginner.

Leave a comment