Values in Lisp

Our first example will demonstrate various value types in Lisp, including strings, integers, floats, and booleans. Here’s the code:

(defun main ()
  ;; Strings, which can be concatenated with the 'concatenate' function
  (format t "~a~%" (concatenate 'string "lisp" "lang"))

  ;; Integers and floats
  (format t "1+1 = ~a~%" (+ 1 1))
  (format t "7.0/3.0 = ~a~%" (/ 7.0 3.0))

  ;; Booleans, with logical operators as you'd expect
  (format t "~a~%" (and t nil))
  (format t "~a~%" (or t nil))
  (format t "~a~%" (not t)))

(main)

Let’s break down the code and explain each part:

  1. We define a main function that will contain our examples.

  2. For string concatenation, we use the concatenate function with the 'string type specifier.

  3. We demonstrate integer addition and floating-point division using the + and / functions respectively.

  4. Boolean operations are shown using and, or, and not functions.

  5. We use format to print the results. The ~a directive is a placeholder for the value to be printed, and ~% adds a newline.

  6. 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:

$ sbcl --script values.lisp
lisplang
1+1 = 2
7.0/3.0 = 2.3333334
NIL
T
NIL

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.