💾 Archived View for gemlog.blue › users › verachell › 1644776575.gmi captured on 2024-03-21 at 17:18:39. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2022-03-01)

-=-=-=-=-=-=-

If you can shell script, you can Lisp

Note: This post is a Gemini space version of my post originally published on January 27, 2020

https version of this post

I've noticed from people's attitudes toward Lisp that many think it is an esoteric and difficult language to learn. Nothing could be further from the truth, but it does depend on what angle you're coming at it from.

If you're coming from C, C++, Javascript, or even Python, then yes Lisp does appear impenetrable at first glance. But if you're coming from shell scripting (e.g. Bash or another Linux or Unix shell) - a skill which most programmers have used at some point - then Lisp is going to be very simple and very familiar for you to learn.

You may be asking, how is this? Well, I'll demonstrate it with a couple of examples.

I'm going to show you a shell command that takes the second column of a space-delimited file test.txt, sorts the resultant data, and shows the second item in the sort.

cut -d" " -f2 test.txt | sort | head -2|tail -1

There is absolutely nothing remarkable about this, as you can see. The results of one command are being piped to the next, reading from left to right. Shell scripting is pretty easy so long as you can keep track of the expected output of each command before piping it to the next.

Well, Lisp is exactly like the shell scripting example above, the main differences are:

To make it clear to see this similarity at a glance for those who don't know any Lisp commands, I'll use mathematical expressions only. Here we'll use the Pythagoras theorem to solve for the length of the hypotenuse. First we'll define the variables *x* and *y* to be 34 and 55 respectively:

(defparameter *x* 34)
(defparameter *y* 55)

Then we'll use Lisp functions as shown below to solve for the hypotenuse:

(sqrt (+ (expt *x* 2)(expt *y* 2)))

For the numbers we used, this results in an output of:

64.66065 

You can see that nested functions in Lisp avoids the need for re-assignment to temporary variables just as piping in the shell , so does nested functions in Lisp. You can see how, working from right to left, you're first getting the square of the input numbers and adding them together. The result of that is passed to the square root function.

You might not think this is particularly surprising; after all, many languages allow slightly complicated mathematical formulas without passing the result to a variable first. But the beauty of Lisp is that this sort of thing can be done with any function, not just mathematical. You can nest functions that manipulate lists or other data structures, making it an incredibly powerful language. Let's look at a quick example of that:

First, we'll define a list; Lisp allows different data types in a list so let's do that!

(defparameter *mylist* '(21 bear "apple" 4 bananas))

Let's suppose we want to remove the 21, multiply it by two, and add it back into the list:

(push (* 2 (pop *mylist*)) *mylist*)

Can you see how we did that? We popped the top item off of *mylist*, multiplied it by two, and pushed it back in. This gives us the expected result of

(42 BEAR "apple" 4 BANANAS)

Note that bear and apple are symbols and as such are converted to uppercase, while apple is a string and is therefore not converted.

We could have done a command with more levels of nesting if we'd wanted to. The limit of how much nesting you want to do in a single line comes down to readability, not to whether Lisp can handle a long line (it can). Again, that's a similarity with shell scripting; you can keep piping as much as you want in one line, but eventually you may wish to break it up for the sake of readability.

As a minor point, notice we have the convenience in Lisp of not having to declare the variable type first, just as is the case in shell scripting.

In many cases I'd argue that the shell scripting actually requires more "work" from your brain than Lisp programming. This is because if your shell scripting files contain rows and columns, you're typically dealing with 2 dimensions every time you shell script.

By contrast, unless you're dealing with 2D or higher dimensional data in Lisp (e.g. arrays or nested lists), most of the commands you use will only require you to think in one dimension. This is the case in the Lisp examples I gave.

I hope these concrete examples have shown you how Lisp is actually not difficult to learn if you look at it from a shell scripting background.

If you can shell script, you can Lisp.

If you program in Ruby (something I'm just starting to learn now), you'll have an easy time in Lisp too, because there are a lot of elements that are similar to Lisp.

If you wish to learn Lisp, the best place to start is the free online book:

Practical Common Lisp by Peter Seibel