Values in Lisp
Our first example will demonstrate various value types in Lisp, including strings, integers, floats, and booleans. Here’s the code:
Let’s break down the code and explain each part:
We define a
main
function that will contain our examples.For string concatenation, we use the
concatenate
function with the'string
type specifier.We demonstrate integer addition and floating-point division using the
+
and/
functions respectively.Boolean operations are shown using
and
,or
, andnot
functions.We use
format
to print the results. The~a
directive is a placeholder for the value to be printed, and~%
adds a newline.Finally, we call the
main
function to execute our examples.
To run this program, save it to a file (e.g., values.lisp
) and use your Lisp interpreter. For example, if you’re using SBCL (Steel Bank Common Lisp), you can run it like this:
This output shows:
- The concatenated string “lisplang”
- The result of adding 1 and 1
- The result of dividing 7.0 by 3.0
- The results of the boolean operations (NIL represents false, T represents true in Common Lisp)
Lisp uses prefix notation for all operations, which might look unusual if you’re coming from other programming languages. However, this consistency is one of Lisp’s strengths, making the language very flexible and powerful.