Skip to content

Instantly share code, notes, and snippets.

@mszegedy
Last active December 1, 2018 19:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mszegedy/4696620 to your computer and use it in GitHub Desktop.
Save mszegedy/4696620 to your computer and use it in GitHub Desktop.
A short introduction to Lisp

Hi! Before I give you my Lisp book, I want to save you some trouble and lay down the basics.

Lisp revolves around three things: parentheses, lists, and functions. Everything else is just glitter. Even calling a function looks almost exactly like a list. Lisp does, after all, get three-quarters of its name from the same word. (I think the P stands for Parser, but I'm not sure.) Reflect briefly on this Hello World program in Lisp:

"Hello, world!"

That's it. Remember how you complained about putting stuff just out in the open in Python, without anything actually doing anything with them? Well, in Lisp you can do that. If you leave a thing like that out in the open that returns a value and isn't getting evaluated, it just prints the value. And I mean why not? What were you doing with that anyway? Trust me, you're better off this way.
The thing about Lisp is that it is an interpreted language. Python is also an interpreted language, but it's difficult to notice. Interpreted means that instead of a compiler that puts the file into a single cohesive unit and spits it out, it reads aloud the source code to itself, one command at a time, and then if it encounters a command, it does the command. This is why in Python your code only crashes on a syntax error when it actually gets to the syntax error in the code. This makes stuff harder to debug, but simpler to write. But you know debugging techniques... right?
So anyway, Lisp is the same deal. Just like in Python, you can type commands directly into the interpreter, or put them in a .lsp file and call the interpreter on it. However, it differs from Python here in that Python processes stuff in files a bit differently than it does interpreter commands: if you leave a value lying around, it leaves it alone. It only prints stuff to the screen after the magical word print. Lisp is different. It processes files exactly like if you typed it into the interpreter in that order and everything. The only difference ends up being that you don't fundamentally need a print function. It evaluates stuff for you and that's it.
One last thing before I actually start teaching you syntax: Lisp is a functional programming language. Yes, blah blah blah, functions and more functions, variables bad functions good, whatever. You've heard it. However, there's one other element to the functional programming mindset: user interaction is dirty. You want to be as asocial as possible. Have as few places in your code that take input or output from the user. I know this seems difficult without things like for loops, which are disgusting imperative programming things that are totally not allowed. However, you will soon discover that you don't need for loops. Your code can stay in one place, and you can watch the programmatic flow beautifully trickle down your tower of functions and if statements, unhindered by waterwheels or places where it squirts out or places where you have to pour more water into the fountain.
Now we can get down to business. Here's a list:

'(foo bar)

It contains two things: FOO and BAR. These are not strings! They're also not variables. They're just values. They don't mean anything in particular. Their capitalization is arbitrary, so the interpreter will print them out as all caps. They're exactly like numbers, but more useless. There's also obviously numbers and floats and bools and stuff. If you ask me how to declare a [int/float/bool/whatever], next time you go to school, I will murder you. (There are actually ways to have both local and global variables, but you only use them if you have very good reason to do so.)
You might have noticed that there was an apostrophe in front of that list. Great observational skills! Let's talk about list syntax. So, a list contains a bunch of things enclosed in parentheses and separated by spaces. If you do not have an apostrophe in front of a thing, then Lisp will treat it like a function name, or worse, a variable. If you put an apostrophe in front of a list, then it automatically distributes into every thing in the list. The list I made before could also be written like this:

('foo 'bar)

This doesn't apply to numbers, mostly. It's not going to try to evaluate the number 6 as a function.

So, what sorts of exciting things happen when we leave off the apostrophes, and let Lisp evaluate stuff as functions? UTTER CHAOS. No, just joking, you shouldn't have to use apostrophes that much. Typically, if you have a pair of parentheses, the Lisp will interpret the first item between the parentheses as a function that you're calling, and the rest of the items as the function's arguments. There are literally no exceptions (except a few that don't count and may not exist anyway). For example, there is the function car that takes a single argument (a list, what else? everything is lists), and returns the first element of it. (You put in a list and it returns its first element). Witness this usage here:

(car '(foo bar))

This will give you FOO. Also, for fun, there actually is a function called print, which does exactly what it sounds like:

(print "Hello, world!")

It's kind of redundant though (unless you're using an interpreter that doesn't interpret values on the spot like I mentioned previously; this is possible, there are two to three main Lisp interpreters out there, and I've only used one or two, CLISP and SBCL). Addition looks like this:

(+ 2 3)

This returns 5. Remember how + works on lists in Python? Tough luck. Use cons:

(cons '(1 2) '(3 4))

This returns '(1 2 3 4). If you have one or more non-list items, it helpfully smoothes it out to one list:

(cons 'a 'b)
(cons 'x '(y z))

These return '(A B) and '(X Y Z) respectively. If asked to concatenate with an empty list, it behaves intelligently:

(cons '(foo bar) '())
(cons 'k '())

These return '(FOO BAR) and '(K) respectively. Notice how it always returns a list! This is not on accident. Lists are the most useful kind of data type in Lisp. Why would you want anything other than a list?

That's all I have time for today. Good night! (Morning, I mean!)

@mszegedy
Copy link
Author

mszegedy commented Feb 3, 2013

Thank you

@danpysmak
Copy link

danpysmak commented Dec 1, 2018

This is a great explanation, thank you 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment