Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
programacion
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)
(* 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))
Zerop es un predicado.
Listas
Computacin simblica.
Estructura de datos recursiva.
Constructor
Selector
Reconocedor
Constructores
Nil
(consxL)
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):
T
USER(30):
NIL
USER(31):
NIL
USER(32):
T
(null nil)
(null '(1 2 3))
(consp nil)
(consp '(1 2 3))
Recursividad estructural
USER(33): (list-length '(2 3 5 7 11 13 17 19))
(defun recursive-list-length (L)
(if (null L)
2: (RECURSIVE-LIST-LENGTH (5 7 11 13 17 19))
3: (RECURSIVE-LIST-LENGTH (7 11 13 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
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
(setf * contador * 0)