Probleme Liste LISP

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Adaugarea unui element la o lista

> (cons '(1 2) '(3 4))


((1 2) 3 4)

> (cons 1 '(2 3 4))


(1 2 3 4)

> (cons 1 (cons 2 nil))


(1 2)

Lista fara primul element


> (rest '(1 2 3 4 5))
(2 3 4 5)

Primul element din lista


> (first '(a b c d))
A

Ultimul element din lista


> (last '(a b c d))
(d)

> (last '(1 2 3))


(3)

Lungimea unei liste


> (list-length '(2 3 5 7 8 22 27 10))
8

> (defun recursive-list-length (L)


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

> (RECURSIVE-LIST-LENGTH '(1 2 3 4))


4

1
Returnerea elementului de pe pozitia n
> (nth 0 '(a b c d))
A

> (nth 2 '(a b c d))


C

> (defun list-nth (N L)


(if (null L)
nil
(if (zerop N)
(first L)
(list-nth (- N 1) (rest L)))))

> (list-nth 2 '(a b c d e f))


C
Verificarea aparenentei unui element la o lista
> (member 3 '(1 2 3 4 5))
(3 4 5)

> (defun list-member (E L)


(cond
((null L) nil)
((eq E (first L)) t)
(t (list-member E (rest L)))))

> (list-member 2 '(1 2 3))


T

> (list-member 6 '(1 2 3))


NIL

2
Concatenarea a doua liste
> (append '(a b c) '(c d e))
(A B C C D E)

> (defun list-append (L1 L2)


(if (null L1)
L2
(cons (first L1) (list-append (rest L1) L2))))

> (list-append '(1 2) '(3 5))


(1 2 3 5)

Intersectia a doua liste


> (defun list-intersection (L1 L2)
(cond
((null L1) nil)
((member (first L1) L2)
(cons (first L1) (list-intersection (rest L1) L2)))
(t (list-intersection (rest L1) L2))))

> (list-intersection '(1 2 3 4) '(3 4 5 6))


(3 4)

Valoarea maxima din lista


> (defun list-max (L)
(loop for i in '(2 1 5 3 4)
maximize i)
)
3
> (list-max '(1 2 0 5 3))
5

Valoarea minima din lista


> (defun list-min (L)
(loop for i in '(2 1 5 3 4)
minimize i)
)

> (list-min '(1 2 0 5 3))


0

You might also like