LISP |
|
|||
LISP ( LISt Processing) |
LISP was invented by John McCarthy in the late 1950's.
The basic building blocks of LISP are Atoms,List and Strings.
List may contain atoms as well as other lists.The basic unit members of a list are called the Top elements.Atoms,lists & strings are the only valid objects in LISP.They are called Symbloic-Expressions or S-Expressions.
LISP uses Prefix notation.
Predefined Numeric Functions |
||
Function Call |
Value Returned |
Remarks |
(+ 3 5 8 4) |
20 |
+ takes zero or more arguments.The sum of zero arguments is 0 |
(- 10 12) |
-2 |
- takes two arguments |
(* 2 3 4) |
24 |
* takes zero or more arguments.The product of no arguments is 1,and the product of 1 argument is the value of the argument. |
(/ 25 2) |
12.5 |
/ takes two arguments. |
Basic List Manipulation Functions |
||
Function Call |
Value Returned |
Remarks |
( car '(a b c)) |
A |
Car takes one argument, a list & returns the first element. |
( cdr '(a b c)) |
(b c) |
Cdr takes one argument, a list & returns a list with the first element removed. |
(cons 'a '(b c)) |
(a b c) |
Cons takes 2 arguments, an element & a list and returns a list with the element inserted at the beginning. |
(list 'a '(b c)) |
(a (b c)) |
List take any number of arguments and returns a list with the arguments as elements. |
Additional List Manipulation Functions |
||
Function Call |
Value Returned |
Remarks |
(append �(a) �(bc)) |
(a b c) |
Merges 2 or more lists into a single list. |
(last �(a b c d)) |
(d) |
Returns a list containing the last element. |
(member �b �(a b d)) |
(b d) |
Returns remainder of second argument list starting with element matching first argument. |
(reverse �(a (b c) d)) |
( d (b c) a) |
Returns list with top elements in reverse order. |
Defining Functions
General Format : (defun name (parm1 parm2 ���.) body)
Defun requires 3 arguments
Example :
->(defun averagethree (n1 n2 n3)
(/ (+ n1 n2 n3) 3) )
AVERAGETHREE
->
To call averagethree,
->( averagethree 10 20 30)
20
->
Predicate Functions
Predicates are functions that test their arguments for some specific condition.
Function Call |
Value Returned |
Remarks |
( atom 'aabb) |
T |
aabb is a valid atom |
( equal 'a ( car '(a b)) |
T |
a equals a but note that ( equal 1 1.0) returns nil |
( evenp 3) |
Nil |
3 is not an even number |
(numberp 20ab) |
Nil |
20ab is not a number |
(oddp 5) |
T |
5 is an odd number |
(zerop 00010) |
Nil |
Argument is not zero |
(greaterp 2 4 27) |
T |
Arguments are succeedingly larger from left to right |
(lessp 5 3 1 2) |
Nil |
Arguments are not succeedingly smaller from left to right. |
(listp '(a)) |
T |
(a) is a valid list |
(null nil) |
T |
Nil is an empty list |
The Conditional Cond
Cond is like if�then�else
General Format:
(cond (<test1> <action1>)
(<test2> <action2>)
(<testk> <actionk>) )
Common LISP provides a conventional if�then�else conditional
General Format:
(it test <then-action> <else-action>)
Logical Functions
Basic logical operations are and , or & not
Input Output
Example :
->(+ 5 (read))
6
11
->
Local Variables
The parameters named as arguments in a function definition are local in scope.
The let and prog constructs permit the creation of local variables.
General Format:
( let ( (var1 val1) (var2 val2) ����) <s-expression> )
where each var i is a different variable name and val i is an initial value assigned to each var i respectively.The s-expression which follow are evaluated in order.
The prog function is similar to let in that the first arguments following it are a list of local variable where each element is either a variable name or a list containing a variable name and its initial value.This is followed by the body of the prog,and any number of s-expression.
Prog executes list s-expression in sequence and returns nil unless it encounters a function call named return.Prog also permits the use of unconditional go statements and labels(atom labels) to identify the go-to transfer locations.
Ó CodeEverywhere.com