Lisp 👽

Lisp (historically LISP; from List Processing) was a programming language created in 1958 by John McCarthy. Many dialects have been created over time, and today Lisp is a generic name to refer to all these cousin programming languages.

The most known Lisp dialects today (IIUC) are Clojure, Common Lisp, and Scheme.

Lisp might be mostly known for its heavy usage of parenthesis to enclose its S-expressions (parenthesis-enclosed hierarchical structures used to represent complex data structures as well as program statements itself).

Vocabulary and WTFs

  • Datum
    a single piece of data (eg.: a string, a number, etc; it's not a strict Lisp term, though it's frequently seem in many Lisp-related texts)
  • Atom
    any data structure that is not a cons cell. Essentially, atoms are the fundamental building blocks that do not contain other LISP objects (numbers, symbols, strings, characters, keywords, and booleans are all atoms). The concept of atoms is useful for distinguishing between compound data structures (like lists) and simpler elements. For example, in an expression, you might want to check if a value is a simple element (atom) or a list
  • Symbol
    a basic data type that represents an identifier or a name. It's a fundamental building block in LISP, used to refer to various entities such as variables, functions, or special keywords
  • S-expression
    is a syntax for for denoting nested, tree-structured code and data. S-expressions are nestable lists made of atoms and/or other S-expressions (aka.: symbolic expression, sexpr, or sexp; see: S-expression)
  • Cons cell
    a two-item tuple that's used to implement lists in Lisp. Items are historically named CAR and CDR (reads coulder), being CAR the one holding the actual value of the const cell, and CDR holding a reference to the next item in the list (when CDR is nil the list has reached its end; it's indeed a linked list, and cons cells are how they call its nodes)

Syntax and data types

How it works in practice.

Comments

They start with a semicolon:

; This is a comment

There are conventions for the number of semicolons to use:

  1. semicolon when right-aligned with code
  2. semicolons when block-aligned with code
  3. semicolons when top-level

Atoms

; A number atom
123
; A string atom
"Hello there"

Symbols

; Define a symbol `name` with value `"Joel"`

(setq name "Joel") ; => "Joel"

name ; => "Joel"

; Here, the symbol `name` is being used with quote - so, a quoted symbol
'name ; => NAME

S-expressions

An S-expression for a list of numbers:

(1 2 3 4 5)

An S-expression for a sum operation:

(+ 1 2) ; => 3

S-expressions can nest:

(+ 1 (+ 2 (+ 3 4))) ; => 10