User:Stevethefiddle/Introduction to Lisp

An extremely brief introduction to Lisp
In Lisp (and therefore Nyquist), everything is an S-Expression, which is just a list of tokens (words) separated by whitespace and enclosed in parentheses. The name of the function is always the first token in an S-Expression, and all of the other tokens are arguments to this function. Here's a simple example:

(setf area (* 3.14159 (expt radius 2)))

Let's break down this example. The outermost S-expression has three members. The first one, setf, is the name of the function (it stands for set-field). setf is used to assign a value to a variable. (There are other similar functions, like set and setq, but setf is the most powerful, so it's the one we'll use in our examples.) After setf comes area, which is the name of the variable we're going to set. Next comes the value to assign to this variable, which in this case is another S-expression.

Lisp doesn't have any special operators for Math functions - they're all functions like everything else, using prefix notation, where the name of the function (or operator) comes before its arguments. So instead of 3*7 for the product of 3 and 7, in Lisp you would write (* 3 7). In Nyquist, the expt (exponent) function raises its first argument to the power of the second argument. Therefore (* 3.14159 (expt radius 2)) means 3.14159 times the square of radius, or the formula for the area of a circle.

Rather than typing in this full expression every time, let's define a function for the area of the circle, that we can call every time we need it:

(defun circlearea (radius) (* 3.14159 (expt radius 2)))

The defun function is used to define a new function. The first argument is the name of the function, in this case circlearea. The second argument is a list of arguments to the function to be defined - this is one of the few cases where you have an S-expression that is not interpreted as a function call. Finally the last expression is the value of the function. Now if we want to compute the area of a circle of radius r, we just need to compute:

(setf area (circlearea r))

An S-expression is just a representation of a list. Lisp uses lists to represent just about everything (the name LISP comes from LISt Processing language), so it's helpful to know how to manipulate lists. Let's start by assigning a list of numbers to a variable. You can't quite do this:

(setf mylist (1 2 3 4 5)) <--  error!

The reason this doesn't work is that whenever Nyquist sees an S-expression, it tries to evaluate it as a function unless you tell it otherwise. Since there's no function named "1" that takes arguments (2 3 4 5), this will generate an error. To tell Lisp that you want to treat an S-expression literally, and not to evaluate it as a function, you quote it. In Nyquist, you can quote a list by putting a single quotation mark before it, like this:

(setf mylist \'(1 2 3 4 5))

Nyquist also provides a list function that you can use to construct lists - this is useful if some of the elements of the list are functions:

(setf mylist (list 1 2 3 4 (sqrt 25)))

To get things off of a list, you can use the first and rest functions. (Traditionally, these were called car and cdr, respectively, but first and rest are much easier to remember. Both sets of names are supported in Nyquist.) The output of (first mylist) is 1, and the output of (rest mylist) is the list (2 3 4 5). So the second element of the list is (first (rest mylist)). Lisp function reference

Here's a list of some of the basic lisp functions you might need. For a complete list of Lisp / Nyquist functions, see the Nyquist version 2.37 Reference Manual.

Math functions

 * (+ a b)	addition
 * (- a b)	subtraction
 * (* a b)	multiplication
 * (/ a b)	division
 * (truncate a b)	round down to integer (floor)
 * (round a) round to nearest integer
 * ''(float a b)	integer to floating-point
 * (rem a b c ...)	remainder
 * (min a b c ...)	minimum
 * (max a b c ...)	maximum
 * (abs a)	absolute value
 * (random n)	random integer between 1 and n-1
 * (sin a b)	sine
 * (cos a b)	cosine
 * (tan a b)	tangent
 * (expt a b)	exponent (a to the power of b)
 * (sqrt a b)	square root
 * (< a b)	test for a less than b
 * (<= a b)	test for a less than or equal to b
 * (> a b)	test for a greater than b
 * (>= a b)	test for a greater than or equal to b
 * (= a b)	test for equality
 * (/= a b)	test for inequality

List functions

 * (first l)	first element of a list (car)
 * (rest l)	rest of the list (cdr)
 * (reverse l)	reverse a list
 * (list a b ...)	construct a list
 * (append l1 l2)	append two lists
 * (length l)	length of a list
 * (maplist function l)	apply a function to every element in a list

Control

 * (if expr expr1 expr2)	if expr is true, evaluates expr1, otherwise evaluates expr2