English 中文(简体)
LISP - Constants
  • 时间:2024-09-17

LISP - Constants


Previous Page Next Page  

In LISP, constants are variables that never change their values during program execution. Constants are declared using the defconstant construct.

Example

The following example shows declaring a global constant PI and later using this value inside a function named area-circle that calculates the area of a circle.

The defun construct is used for defining a function, we will look into it in the Functions chapter.

Create a new source code file named main.psp and type the following code in it.

(defconstant PI 3.141592)
(defun area-circle(rad)
   (terpri)
   (format t "Radius: ~5f" rad)
   (format t "~%Area: ~10f" (* PI rad rad)))
(area-circle 10)

When you cpck the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is.

Radius:  10.0
Area:   314.1592
Advertisements