Lisp

λ

Lisp (list processing) is a functional, interpreted programming language. It is based on lambda calculus and the principle of "everything is a list".

Basic Syntax

Lisp only uses simple parentheses to nest functions. The operator is always on the left and the operands on the right. Example of small calculation:

(+ 3 4)

Large calculation:

(* 4 (+ 4 6 (/ 4 2)))

will be evaluated to

(* 4 (+ 4 6 2)

(* 4 12)

48

Means of Abstraction

Definition of data

(define a 7)

equals

(define a (+ 3 4))

Usaging defined data:

(+ a 10)

outputs 17.

Definition of procedure

(define (square x) (* x x))

equals

(define square (lambda (x) (* x x)))

List Structure

    _ | _      _ | _      _ | _ 
-> |_| |_| -> |_| |_| -> |_| |/|
    |          |          |     
    1          2          3     

(cons x y) - constructs a pair

(car p) - (car (cons x y)) - selects first part of pair

(cdr p) - (cdr (cons x y)) - selects second part of pair

(cadr p) - car of cdr; all the basic combinations of car and cdr are defined

(list x y z) - convention for representing a sequence of cons:

(cons 1              
	(cons 2          
		(cons 3 nil)))

map - creates a new list with value applied to each parameter

for-each - applies procedure to each parameter, returns result

let - local name, does not interfere with definition

(let ((a 10))
(+ a a)

Everything in lisp can be represented using list structure, including procedures themselves.

(+ 2 5)
    _ | _      _ | _      _ | _ 
-> |_| |_| -> |_| |_| -> |_| |/|
    |          |          |     
    +          2          5     

Conditions

(define (abs x)               
	(cond	((< x 0) (- x))
			((= x 0) 0)       
			((> x 0) x)))     

equals

(define (abs x)
	(if	(< x 0)
		(- x)  
		x))