Вы находитесь на странице: 1из 20

Lenguajes de Programacin

Lenguajes funcionales: LISP


M.C Said Zamora

Expresiones
LISP evala expresiones definidas por el usuario.
USER(1): (* 2 (cos 0) (+ 4 6))
Formas, f(x) equivale a (f x)

Funciones numricas
(+ x1 x2 ... xn)
(* x1 x2 ... xn)
(- x y)
(/ x y)
(rem x y)
(abs x)
(max x1 x2 ... xn)
(min x1 x2 ... xn)

Funciones
USER(2): (defun double (x) (* x 2))
USER(3): (double 3)

Recursividad
(defun factorial (N)
Factorial de N."
(if (= N 1)

1
(* N (factorial (- N 1)))))

Operadores relacionales
(= x y)
(/= x y)
(< x y)
(> x y)
(<= x y)
(>= x y)

Recursividad mltiple
(defun fibonacci (N)
"N numero de Fibonacci."
(if (or (zerop N) (= N 1))

1
(+ (fibonacci (- N 1)) (fibonacci (- N 2)))))

Zerop es un predicado.

Operadores lgicos y funciones predefinidas

(1+ x)
(1- x) (- x 1)
(zerop x)
(plusp x)
(minusp x)
(evenp x)
(oddp x)

(+ x 1)
(= x 0)
(> x 0)
(< x 0)
(= (rem x 2) 0)
(/= (rem x 2) 0)

(or x1 x2 ... xn)


(and x1 x2 ... xn)
(not x)

Listas
Computacin simblica.
Estructura de datos recursiva.
Constructor
Selector

Reconocedor

Constructores
Nil
(cons x L)
USER(21): (cons 1 (cons 2 nil))
(quote (2 3 5 7 11 13 17 19))

'(2 3 5 7 11 13 17 19)

Selectores
(first '(2 4 8))
(rest '(2 4 8))

Reconocedores
USER(29): (null nil)
T
USER(30): (null '(1 2 3))
NIL
USER(31): (consp nil)
NIL
USER(32): (consp '(1 2 3))
T

Recursividad estructural
USER(33): (list-length '(2 3 5 7 11 13 17 19))
(defun recursive-list-length (L)
(if (null L)

0
(1+ (recursive-list-length (rest L)))))

USER(40): (trace recursive-list-length)

(RECURSIVE-LIST-LENGTH)

USER(41): (recursive-list-length '(2 3 5 7 11 13 17 19))

0: (RECURSIVE-LIST-LENGTH (2 3 5 7 11 13 17 19))

1: (RECURSIVE-LIST-LENGTH (3 5 7 11 13 17 19))

2: (RECURSIVE-LIST-LENGTH (5 7 11 13 17 19))

3: (RECURSIVE-LIST-LENGTH (7 11 13 17 19))

4: (RECURSIVE-LIST-LENGTH (11 13 17 19))

5: (RECURSIVE-LIST-LENGTH (13 17 19))

6: (RECURSIVE-LIST-LENGTH (17 19))

7: (RECURSIVE-LIST-LENGTH (19))

8: (RECURSIVE-LIST-LENGTH NIL)

8: returned 0

7: returned 1

6: returned 2

5: returned 3

4: returned 4

3: returned 5

2: returned 6

1: returned 7

0: returned 8

Smbolos
No diferencia maysculas y minsculas
`t = t

Ensimo
USER(59): (nth 0 '(a b c d))
A
USER(60): (nth 2 '(a b c d))
C

Member
USER(64): (member 'b '(hola)) ;
NIL
USER(65): (member y '( y hola)) ;
(y hola)

(= x y)
(eq x y)
(eql x y)
(equal x y)
(equalp x y)

Reverse L
USER(1): (reverse '(1 2 3 4))
(4 3 2 1)
USER(2): (reverse '(1 (a b) (c d) 4))
(4 (c d) (a b) 1)
USER(3): (reverse nil)
NIL

Estado del programa, variables.


(defparameter * contador * 0)
(defparameter *umbral* 4)
(defun contador ()

(if (>= (1+ *contador*) * umbral *)


(setf * contador * 0)
(setf * contador * (1+ * contador *))))

(defun valor ()
*counter*)
(defun valorumbral ()
*umbral*)

Вам также может понравиться