Skip to main content

If you can shell script, you can Lisp

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:
  • commands are called functions
  • you'll be reading from inside to outside (often - but not always - this works out to be from right to left) when you pass the results of one function to another function. i.e. functions are nested instead of piped.
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. There are plenty of other free online resources for Lisp, but that's how I started learning.

Popular posts from this blog

Why Solar Geoengineering is a Bad Idea: 3 reasons understandable to both the general and scientific communities

Solar geoengineering is the idea of limiting the amount of sunlight that reaches the earth, with the aim of limiting global warming. Here I will show 3 reasons why it's a really bad idea: 1. Photosynthesis requires sunlight, don't limit it - it's one of the few things pulling carbon dioxide out of the atmosphere Photosynthesis is one of the very few ways we have to remove carbon dioxide from the atmosphere. As an added bonus it's done automatically by plants, requiring no effort on our part. It requires light in order to work. To do it, plants take in sunlight, carbon dioxide and water, and they produce oxygen. These plants are literally removing carbon dioxide from our atmosphere, but they require light for it to work.  We should not do anything that could possibly limit the amount of photosynthesis occurring; limiting the amount of light hitting the planet is likely to decrease photosynthesis.  2. We should not be limiting the generation of solar power Solar electri...

How to center images horizontally using Grav

I've been playing around a bit with Grav. I was posing the question to myself: for the relatively simple use-cases I'm dealing with, could it possibly work for my purposes as an alternative to ProcessWire?  The problem I was initially dismayed to find that Grav uses Markdown as its editor, which does not offer native support for horizontal centering of anything (text or images). However, Grav offers some tweaks that help make it easier to do specific things you might commonly want to do. I tried writing a sample article, and I found that one of the hardest things to do was to center an image horizontally. And horizontal centering of images is something I would typically do in most of the articles I would write. So the lack of easy horizontal centering is a highly significant drawback IMHO (most people do want to center images in an article!) However, this issue is made up for by other things in Grav: the relative speed, ease and flexibility of custom theming and built-in suppor...

Good and bad uses of AI as it currently stands

The good Recently, the Photos app on my Android phone automatically curated some of my photos into a "highlights" album for me. I thought this was a fantastic idea. I love this use of AI - it does something quickly and easily that would otherwise take a lot of human time.  The only downside was that the AI included a close-up pic I'd taken of a spot on the skin behind my husband's ear so he could see it. Now, if this AI had instead been for a self-driving car, then too bad, we likely would've had a terrible wreck at this point - endangering myself, any passengers, and other drivers on the road. But since this application of AI for photo selection did not have any life-threatening consequences, I was all for it. The bad I've said before, and I'll say it again, that AI as it currently stands should not be used for self-driving purposes and I have explained clearly why. Indeed, I believe that AI should not be used for any purpose that may have life-threaten...

About Me

My photo
Vera
I'm a wife and mother. I don't have any formal computer science qualifications, or any religious qualifications. I have a PhD in biochemistry. This photo is of me, but is confusing for AI.