Skip to content

Commit 5fb37dc

Browse files
author
Willsem
committed
🆕 add source code of lab_08
1 parent a2d8e7d commit 5fb37dc

File tree

6 files changed

+103
-0
lines changed

6 files changed

+103
-0
lines changed

lab_08/src/1.lisp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
(defun is_palindrom (lst)
2+
(equal lst (reverse lst))
3+
)
4+
5+
(is_palindrom "abccba") ;;; T
6+
(is_palindrom "abcgba") ;;; Nil

lab_08/src/2.lisp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
(defun swap-first-last (lst)
2+
(cond
3+
((null lst) Nil)
4+
((eql (length lst) 1) lst)
5+
(T
6+
(append
7+
(last lst)
8+
(mapcar #'(lambda (el _) el) (cdr lst) (cddr lst))
9+
(list (first lst))
10+
)
11+
)
12+
)
13+
)
14+
15+
(swap-first-last ()) ;;; Nil
16+
(swap-first-last '(1)) ;;; (1)
17+
(swap-first-last '(1 2)) ;;; (2 1)
18+
(swap-first-last '(1 2 3 4 5)) ;;; (5 2 3 4 1)

lab_08/src/3.lisp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
(defun swap-two-elements (a b lst)
2+
(cond
3+
((eql a b) lst)
4+
((> a b) (swap-two-elements b a lst))
5+
(T
6+
(append
7+
(subseq lst 0 a)
8+
(list (nth b lst))
9+
(subseq lst (+ a 1) b)
10+
(list (nth a lst))
11+
(subseq lst (+ b 1))
12+
)
13+
)
14+
)
15+
)
16+
17+
(swap-two-elements '2 '4 '(1 2 3 4 5)) ;;; (1 2 5 4 3)
18+
(swap-two-elements '1 '4 '(1 2 3 4 5)) ;;; (1 5 3 4 2)
19+
(swap-two-elements '3 '2 '(1 2 3 4 5)) ;;; (1 2 4 3 5)

lab_08/src/4.lisp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
(defun swap-to-left-one (lst)
2+
(append (mapcar #'(lambda (el _) el) (cdr lst) lst) (list (car lst)))
3+
)
4+
5+
(defun swap-to-left (lst k)
6+
(cond
7+
((<= k 0) lst)
8+
(T (swap-to-left (swap-to-left-one lst) (- k 1)))
9+
)
10+
)
11+
12+
(swap-to-left '(1 2 3 4 5) 2) ;;; (3 4 5 1 2)
13+
(swap-to-left '(1 2 3 4 5) 5) ;;; (1 2 3 4 5)
14+
15+
(defun swap-to-right-one (lst)
16+
(append (last lst) (mapcar #'(lambda (el _) el) lst (cdr lst)))
17+
)
18+
19+
(defun swap-to-right (lst k)
20+
(cond
21+
((<= k 0) lst)
22+
(T (swap-to-right (swap-to-right-one lst) (- k 1)))
23+
)
24+
)
25+
26+
(swap-to-right '(1 2 3 4 5) 2) ;;; (4 5 1 2 3)
27+
(swap-to-right '(1 2 3 4 5) 5) ;;; (1 2 3 4 5)

lab_08/src/5.lisp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
(defun number_multiplication_car (lst n)
2+
(mapcar #'(lambda (el) (* el n)) lst)
3+
)
4+
5+
(number_multiplication_car '(1 2 3 4) '5) ;;; (5 10 15 20)
6+
7+
(defun multiplication_car (lst n)
8+
(mapcar #'(lambda (el) (if (numberp el) (* el n) el)) lst)
9+
)
10+
11+
(multiplication_car '(1 2 3 4) '5) ;;; (5 10 15 20)
12+
(multiplication_car '(a 2 b 4) '5) ;;; (A 10 B 20)

lab_08/src/6.lisp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
(defun select_between (a b lst)
2+
(sort (reduce #'
3+
(lambda (lst el)
4+
(append
5+
(if (numberp lst)
6+
(if (and (<= a lst) (>= b lst))
7+
(list lst)
8+
)
9+
lst
10+
)
11+
(if (and (<= a el) (>= b el))
12+
(list el)
13+
)
14+
)
15+
) lst
16+
) #'<)
17+
)
18+
19+
(select_between '1 '10 '(40 5 2 -6 3 1 8)) ;;; (1 2 3 5 8)
20+
(select_between '-10 '5 '(-6 -2 -11 8 0 1 -10 -7 100 20 -4))
21+
;;; (-10 -7 -6 -4 -2 0 1)

0 commit comments

Comments
 (0)