Solution exercises from P01 to P07
This commit is contained in:
9
p01.el
Normal file
9
p01.el
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
;; P01 (*) Find the last box of a list
|
||||||
|
(defun my-last (lst)
|
||||||
|
(cond
|
||||||
|
((eq lst '()) '())
|
||||||
|
((eq (cdr lst) '()) lst)
|
||||||
|
(t
|
||||||
|
(my-last (cdr lst)))))
|
||||||
|
|
||||||
|
(my-last '(a b c d)) ; '(d)
|
||||||
9
p02.el
Normal file
9
p02.el
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
;; P02 (*) Find the last but one box of a list
|
||||||
|
(defun my-but-last (lst)
|
||||||
|
(cond
|
||||||
|
((eq lst '()) '())
|
||||||
|
((eq (cddr lst) '()) lst)
|
||||||
|
(t
|
||||||
|
(my-but-last (cdr lst)))))
|
||||||
|
|
||||||
|
(my-but-last '(a b c d)) ; '(c d)
|
||||||
9
p03.el
Normal file
9
p03.el
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
;; P03 (*) Find the k'th element of a list
|
||||||
|
(defun element-at (lst k)
|
||||||
|
(cond
|
||||||
|
((eq lst '()) '())
|
||||||
|
((eq k 1) (car lst))
|
||||||
|
(t
|
||||||
|
(element-at (cdr lst) (- k 1)))))
|
||||||
|
|
||||||
|
(element-at '(a b c d) 3) ; c
|
||||||
10
p04.el
Normal file
10
p04.el
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
;; P04 (*) Find the number of element of a list
|
||||||
|
(defun my-length (lst)
|
||||||
|
(defun my-length-rec (lst res)
|
||||||
|
(cond
|
||||||
|
((eq lst '()) res)
|
||||||
|
(t
|
||||||
|
(my-length-rec (cdr lst) (+ 1 res)))))
|
||||||
|
(my-length-rec lst 0))
|
||||||
|
|
||||||
|
(my-length '(a b c)) ; 3
|
||||||
10
p05.el
Normal file
10
p05.el
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
;; P05 (*) Reverse a list
|
||||||
|
(defun my-reverse (lst)
|
||||||
|
(defun my-reverse-rec (lst res)
|
||||||
|
(cond
|
||||||
|
((eq lst '()) res)
|
||||||
|
(t
|
||||||
|
(my-reverse-rec (cdr lst) (cons (car lst) res)))))
|
||||||
|
(my-reverse-rec lst '()))
|
||||||
|
|
||||||
|
(my-reverse '(a b c)) ; '(c b a)
|
||||||
7
p06.el
Normal file
7
p06.el
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
;; P06 (*) Find out whether a list is a palindrome.
|
||||||
|
(defun palindrome (lst)
|
||||||
|
(equal lst (reverse lst)))
|
||||||
|
|
||||||
|
(palindrome '(x a m a x)) ; t
|
||||||
|
(palindrome '(a n n a)) ; t
|
||||||
|
(palindrome '(f a b i o)) ; nil
|
||||||
Reference in New Issue
Block a user