Chapter 3. Exploring the Syntax of Lisp Code

As you’ve learned so far, Lisp commands must be entered in a rather unorthodox way, with parentheses surrounding each command. In this chapter, we’ll explore why Lisp works this way.

To understand why any language—whether it’s a programming language or a human language—looks a certain way, we need to begin with two concepts from the field of linguistics: syntax and semantics.

Syntax and Semantics

Here is a typical sentence in the English language:

My dog ate my homework.

This sentence has the correct syntax for a sentence in English. The syntax of a piece of text represents the basic rules that it needs to follow to be a valid sentence. Here are some of the rules of sentences in the English language that this text obeys:

  • The sentence ends in a punctuation mark.

  • The sentence contains a subject and a verb.

  • The sentence is made up of letters in the English alphabet (as opposed to Egyptian hieroglyphics or Sumerian cuneiform).

However, there is more to a sentence than just its syntax. We also care about what the sentence actually means. When we talk about the semantics of a sentence, we’re referring to its meaning.

For instance, here are some sentences that all roughly have identical semantics:

My dog ate my homework.
The canine, which I possess, has consumed my school assignment.
Der Hund hat meine Hausarbeit gefressen.

The first two are just different ways of saying the same thing in English. The third sentence is in German, but it still has the same meaning and, hence, semantics as the first two.

The same distinction between these two ideas exists in programming languages. For instance, here is a valid line of code written in C++:

((foo<bar>)*(g++)).baz(!&qux::zip->ding());

This line of code obeys the rules of C++ syntax. To make my point, I put in a lot of weird syntax that is unique to C++, which differentiates it from other languages. If you were to place this line of code in a program of another programming language with a different syntax, it would probably be invalid and cause an error.

Of course, this C++ programming code also means something. If we were to put this line of code in a C++ program (in the proper context), it would cause your computer to do something. The actions that a program performs are the semantics of the program. It is usually possible to write a program that has the same semantics in different programming languages; that is, the program will do the same thing in both languages.

What all this boils down to is that most programming languages have similar semantic powers. However, basic Lisp code is different from code in any other major programming language in that it has a far simpler syntax. Having a simple syntax is a defining feature of the Lisp language.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.31.22