The Lisp Tutorials have been written for version 1.0. Examples and practices that are described in this document may not be up to date.
This Lisp dialect is a statically-typed programming language, which means that all variables must first be declared before they can be used. The declaration of a new variable involves stating it's type and name:
;Listing 001: Exemplary declaration and initialization
(void main () (
(int field 1)
))
Doing this tells your program that a variable named "field" exists, holds numerical data and has an initial value of "1". A variable's data type determines the values it may contain, plus the operations that may be performed with it. In addition to int
, this Lisp dialect supports several other primitive data types. A primitive type is predefined by the language and is named by a reserved keyword. The supported primitive data types are:
int
: Theint
data type is a 32-bit signed integer, which has a minimum value of -231 and a maximum value of 231-1.double
: Thedouble
data type is a double-precision 64-bit floating point. It accommodates seven digits. Its range is approximately 5.0 × 10-345 to 1.7 × 10-308.bool
: Thebool
data type has only two possible values:T
(true) andNIL
(false). This data type represents one bit of information.char
: Thechar
data type is a single 8-bit ASCII character. It's minimum value is 0 and it's maximum value is 255.string
: Thestring
has no specified size or range. It is a single text which contains an array ofchar
-type values, with any length and is handled, unlike in other programming languages, like a primitive data type.
It's not always necessary to assign a value when a variable is declared. Variables that are declared but not initilaized will be set to a defaul type by the interpreter. Generally, these default values will be 0, depending on the data type. Relying on such default values is overall considered bad programming.
The following chart summarizes the default values for the earlier mentioned data types:
Data Type | Default Value | Remarks |
---|---|---|
int |
0 |
- |
double |
0.0 |
- |
bool |
NIL |
Value is set to false |
char |
' ' |
Value is set to a whitespace. |
string |
"" |
Value is set to an empty string. |
In the following, the declaration and initialization of a variable is described using the int
keyword. Other data types work in the exact the same way.
A integer variable can be declared as follows:
;Listing 002: Declaration of integer variable
(void main () (
(int myVar)
))
By doing so, a variable named "myVar" is declared, which can store numerical values, since it is declared using the int
keyword. The variable is not initialized in this statement, which makes the interpreter assign the default value of 0 to the variable.
If you want to initialize the variable, there are two possible ways to do so:
;Listing 003: Declaration and initialization of integer variable
(void main () (
;Declaration and initialization in a single statement
(int myVar 15)
;Declaration and initialization in two seperate statements
(int myNewVar)
(set myNewVar 15)
))
When doing this, the variable will be initialized with the value 15.
A LISP function must return a value when using any of the earlier mentioned primitive data types. The keyword that indicates the return type of a function takes the first place in the list that resembles the function's definition:
;Listing 004: Exemplary definition of a function
(string getName () (
;^^^^^^ <- Indicates that the function returns a String
(return "John Doe")
; ^^^^^^^^^^ <- String is returned using the "return" keyword
))
However, it is not always neccessary to return a value. Utilizing the data type void
, which does only work with functions, no value will be returned.