Tutorial Lisp

Páginas: 11 (2668 palabras) Publicado: 19 de abril de 2012
LISP
ADICION
CL-USER 8 : 1 > (+ 5 10 15)

30 Data Types: Atoms
CL-USER 9 : 2 > (setq anio_nacimiento 1987)

1987
CL-USER 10 :1 > (+ 5 5)

10
CL-USER 11 : 4 > (+ anio_nacimiento 24)

2011

TIPO DE DATOS
Encontramos dos: Atomos y Listas

• Atoms: a, 7, tom, my-age, nil, T
– Asignamos un valor a un átomo con setq y/o setf CL-USER 12 : 4 > (setq my-name "unmsm" ) unmsm CL-USER 13 : 4 > (setfmy-teacher "Hugo Vega" ) Hugo Vega

• Lists:

(a), (+ 6 7), (a (e f g) h), (), nil

– nulo (nil) se considera de ambos modos, es un átomo y una lista. Es equivalente a una lista vacia (). CL-USER 22 : 7 >() NIL

– Evaluando una lista siempre invoca a una función:(función-nombre arg1 … argn)

CL-USER 2 > (/ 8 4 )=2 CL-USER 14 : 3 > (setf c 4)=4 CL-USER 20 : 4 > (max c 2) = 4

FUNCIONES PRIMITIVAS
• Seincluye muchas figuras en las funciones: +, *, -, /, max, min, sqrt • De preferencia, se incluye estas figuras en listas de operaciones. – cons builds a list out of data and a tail.

CL-USER 29 : 8 > (setq nombre "Miguel" ) Miguel CL-USER 31 : 8 > (cons nombre (cons "Cruz" nil )) (Miguel Cruz) CL-USER 32 : 8 > (cons 5 nil) 5 CL-USER 33 : 8 > (cons 5 (cons 3 nil)) (5 3) CL-USER 34 : 8 > (setq a 15)CL-USER 35 : 8 > (cons a nil) 15

FUNCIONES PRIMITIVAS: CONSTRUYENDO LISTAS
• Necesitas decir al intérprete que no evalue el átomo “you”. Para ello,usamos ‘.

CL-USER 1 > (cons '"yo" '(aprendo)) = ("yo" APRENDO)

CL-USER 12 : 1 > (cons 'a (cons 'b (cons 'c 'd))) = (A B C . D)
– list y append son funciones que toman algun numero de argumentos:

CL-USER 5 : 2 > (list '(A) 'better 'world '(for) '"us")= ((A) BETTER WORLD (FOR) "us") CL-USER 1 >(list 1) = (1) CL-USER 2 > (list* 1) = 1 CL-USER 3 > (setq a 1) = 1 CL-USER 4 > (list a 2) = (1 2) CL-USER 1 >(append '(* 8 2) '(/ 8 2) '(f i s i) '(- m w )) = (* 8 2 / 8 2 F I S I - M W) CL-USER 7 > (cons ’a nil)= a CL-USER 8 > (cons ’h ’(o l a)) (hola) CL-USER 9 > (list ’x ’y ’z ) ( x y z) CL-USER 10 > (list ’p ’(q r s )) (p q r s) CL-USER 11 > (append’(a b c)’(p q r )’(y z)) (a b c p q r y z)

NOTACION BACKQUOTE Y COMMA
La backquote "`" significa que en la expresión a continuación nada se evaluará a no ser que vaya precedido de "," o de ",@", la diferencia está en que la expresión precedida de ",@" debe evaluar a una lista, y el efecto es que el nivel superior de paréntesis desaparecerá. CL-USER 1 > (setf test1 '(a test)) = (A TEST) CL-USER 2> `(this is,test1) =(THIS IS (A TEST))

CL-USER 3 > `(this is,@test1) = (THIS IS A TEST) CL-USER 4 > `(this is.,test1) =(THIS IS. (A TEST)) CL-USER 5 > `(this is . ,test1) = (THIS IS A TEST) CL-USER 17 : 5 > `(cond ((numberp ,1) ,@2) (t (print ,1) ,@2)) = (COND ((NUMBERP 1) . 2) (T (PRINT 1) . 2)) CL-USER 18 > (list ’3 ’7 (sqrt 25) ’a ’b ) (3 7 5 a b) CL-USER 19 > (List a b,(* 4 3) x y ) (a b 12x y )

FUNCIONES PRIMITIVAS: ACCESANDO A LISTAS
Una vez que la lista esta construida, ¿cómo accesamos a sus miembros?  Las funciones first, second, third, fourth, fifth, sixth, seventh, eighth, ninth, y tenth acceden al primer, segundo, tercer, cuarto, quinto, sexto, séptimo, octavo, novena y décimo elemento de la lista, respectivamente.

CL-USER 22 : 4 > (setq lst '(1 2 3 (4 5 6) ((V)) vi 7 8 910)) = (1 2 3 (4 5 6) ((V)) VI 7 8 9 10) CL-USER 23 : 4 > first lst = 1 CL-USER 25 : 5 > tenth lst = 10 CL-USER 26 : 5 > fifth lst = ((V)) CL-USER 27 : 5 > (second (fourth lst)) = 5

Rest: Realiza una operacion similar al cdr, pero se complementa con el first. CL-USER 28 : 5 > (rest '(1)) = NIL CL-USER 30 : 6 > (rest '(a . b)) = B

CL-USER 28 : 5 > (first ’(6 7 9)) 6 CL-USER 28 : 5 > (first ’((xy) 8 9) (x y)

CL-USER 28 : 5 > (rest ’(f i s i)) (i s i) CL-USER 28 : 5 > (rest ’((x y) 3 5)) (3 5) CL-USER 28 : 5 > (caar ’((h e) l l o)) (h) CL-USER 28 : 5 > (cdar ’(((h e) l l o) x y ) (o)

CAMBIANDO VALORES ATOMICOS
CL-USER 18 : 5 > (setf myidea '(save the world)) = (SAVE THE WORLD) CL-USER 19 : 5 > myidea = (SAVE THE WORLD) CL-USER 20 : 5 > (cons 'We myidea) = (WE SAVE THE WORLD) CL-USER...
Leer documento completo

Regístrate para leer el documento completo.

Estos documentos también te pueden resultar útiles

  • Lisp
  • lisp
  • Lisp
  • lisp
  • lisp dicertacion
  • programación Lisp
  • Examen LISP
  • ejercicios de lisp

Conviértase en miembro formal de Buenas Tareas

INSCRÍBETE - ES GRATIS