Saturday, December 14, 2019

Lisp Expressions vs. Lisp Forms

Lisp Expressions vs. Lisp Forms

1 Lisp Expressions vs. Lisp Forms

1.1 General

During my Lisp studies (Emacs Lisp and Clojure), I often encountered the terms: Lisp "Expression" and Lisp "Form" and I started wondering what is the difference since most literature seems to use both terms interchangeable.

An excerpt from: “Living Clojure” - Carin Meier

"An expression is [Lisp] code that can be evaluated for a result, and a form is a valid expression that can be evaluated"

In other words an expression is a valid input for the LISP `READ` (REPL) while a form is a valid input for both the `READ` and the `EVAL` part of the (REPL).

1.2 Examples

The below is a valid form (valid both for the `READ` and the `EVAL`)

(+ 1 2)

;; => 3

The below (assuming the 'foo' function is not defined) is a valid expression but not a valid form, it can be read but not evaluated since 'foo' is not defined:

(foo 1 2)

;; => Syntax error compiling at (REPL:1:1). Unable to resolve symbol: foo in this context

No comments:

Post a Comment