Docsity

Lisp Functions: Basic Operations and Syntax, Study notes of Programming Languages

Toggle sidebar
bg1
LISP FUNCTIONS
Summarized below are several basic functions used in the Lisp programming language. In some cases, both a
short form and a long form are given (separated by or).
Basic List Operations
(car X) - return the head (first element) of the list X
(cdr X) - return the tail of the list X (X with the first element removed)
(cons X Y) - return the list created by inserting the list X onto the front of list Y
(append X Y) - return the list created by concatenating lists X and Y
(list X1X2... Xn) - return the list created by concatenating lists X1, X2, ... Xn
(โ€™ X) or (quote X) - return the list X (treated as a literal)
Arithmetic Operations
(+ E1E2... En) - return E1+E2+. . . +En
(โˆ’E1E2... En) - return E1โˆ’E2โˆ’. . . โˆ’En
(* E1E2... En) - return E1*E2*. . . *En
(/ E1E2... En) - return E1โ„E2โ„. . . โ„En
(1+ X) - return X + 1
(1โˆ’X) - return X โˆ’1
Relational Operations
(zerop X) - if X = 0 then return T else return NIL
(relation X Y) - if X relation Y then return T else return NIL (relation is =, /=, <,<=, >,>=)
(equal X Y) - if X = Y then return T else return NIL
(eq X Y) - if X and Y are the same object, then return T else return NIL
Logical Operations
(and X1X2... Xn) - return X1ฮ›X2ฮ›... ฮ›Xn
(or X1X2... Xn) - return X1VX2V... VXn
(not X) - return ยฌX
pf2

Partial preview of the text

Download Lisp Functions: Basic Operations and Syntax and more Study notes Programming Languages in PDF only on Docsity!

LISP FUNCTIONS

Summarized below are several basic functions used in the Lisp programming language. In some cases, both a short form and a long form are given (separated by or ).

Basic List Operations (car X) - return the head (first element) of the list X (cdr X) - return the tail of the list X (X with the first element removed) (cons X Y) - return the list created by inserting the list X onto the front of list Y (append X Y) - return the list created by concatenating lists X and Y (list X 1 X 2 ... Xn) - return the list created by concatenating lists X 1 , X 2 , ... Xn (โ€™ X) or (quote X) - return the list X (treated as a literal)

Arithmetic Operations (+ E 1 E 2 ... En) - return E 1 +E 2 +...^ +En (โˆ’ E 1 E 2 ... En) - return E 1 โˆ’E 2 โˆ’...^ โˆ’En (* E 1 E 2 ... En) - return E 1 *E 2 *... *En (/ E 1 E 2 ... En) - return E 1 โ„ E 2 โ„...^ โ„ En (1+ X) - return X + 1 (1โˆ’ X) - return X โˆ’ 1

Relational Operations (zerop X) - if X = 0 then return T else return NIL ( relation X Y) - if X relation Y then return T else return NIL ( relation is =, /=, <, <=, >, >=) (equal X Y) - if X = Y then return T else return NIL (eq X Y) - if X and Y are the same object, then return T else return NIL

Logical Operations (and X 1 X 2 ... Xn) - return X 1 ฮ› X 2 ฮ› ... ฮ› Xn (or X 1 X 2 ... Xn) - return X 1 V X 2 V ... V Xn (not X) - return ยฌ X

Control Operations (setq X Y) - assign the value of Y to X and return the value of Y (set X Y) - same as setq except that X can be a computed value (cond (test 1 expression 1 ) if test 1 then return expression 1 (test 2 expression 2 ) else if test 2 then return expression 2 ... ... (testn expressionn)) else if testn then return expressionn (print X) - print X

Function Manipulation Operations (defun F (X 1 X 2...^ Xn) E) - define function F on arguments X 1 , ..., Xn to be the value of expression E (apply F L) - apply the function denoted by F to the argument list L (eval E) - evaluate the expression denoted by E (prog (X 1 X 2...^ Xn) E 1 E 2...^ Em) - define local variables X 1 , ..., Xn to be used in evaluating expressions E 1 , ..., Em, the value of the last being returned as the value of the PROG expression (return E) - return the value of E (usually used in a prog expression)