llsheng_73
发表于 2014-1-13 13:27:54
个人觉得最好的要依赖于递归,它对于真正理解算法没帮助,相反的,把一些经典的算法从递归剥离出来可能对于算法的理解会更深入些,个人看法,希望别介意
lanjqka
发表于 2014-1-15 10:58:05
;|
_$$ (czr '(0 1 2 3 4))
4
|;
(defun czr (lst)
(if (null (cadr lst))
(car lst)
(czr (cdr lst))
)
)
;|
_$$ (cztr '(0 1 2 3 4))
3
|;
(defun cztr (lst)
(if (null (caddr lst))
(car lst)
(cztr (cdr lst))
)
)
;|
_$$ (czttr '(0 1 2 3 4))
2
|;
(defun czttr (lst)
(if (null (cadddr lst))
(car lst)
(czttr (cdr lst))
)
)
;|
_$$ (ctr '(0 1 2 3 4))
(0 1 2 3)
|;
(defun ctr (lst)
(if (null (cadr lst))
nil
(cons (car lst) (ctr (cdr lst)))
)
)
;|
_$$ (cttr '(0 1 2 3 4))
(0 1 2)
|;
(defun cttr (lst)
(if (null (caddr lst))
nil
(cons (car lst) (cttr (cdr lst)))
)
)
;|
_$$ (ctttr '(0 1 2 3 4))
(0 1)
|;
(defun ctttr (lst)
(if (null (cadddr lst))
nil
(cons (car lst) (ctttr (cdr lst)))
)
)
lanjqka
发表于 2014-1-15 11:14:52
llsheng_73 发表于 2014-1-13 13:27 static/image/common/back.gif
个人觉得最好的要依赖于递归,它对于真正理解算法没帮助,相反的,把一些经典的算法从递归剥离出来可能对于 ...
不介意
只是学习递归
不希望做争论
lanjqka
发表于 2014-1-16 15:04:21
本帖最后由 lanjqka 于 2014-1-16 15:27 编辑
;|
_$$$$$$$$ (powerset '(1 2 3))
((1 2 3) (1 2) (1 3) (1) (2 3) (2) (3) nil)
_$$$$$$$$ (powerset '(0 1 2 3))
((0 1 2 3) (0 1 2) (0 1 3) (0 1) (0 2 3) (0 2) (0 3) (0) (1 2 3) (1 2) (1 3) (1) (2 3) (2) (3) nil)
|;
(defun powerset (lst)
(if (null lst)
(list nil)
(setpower (list (car lst)) (powerset (cdr lst)))
)
)
;|
_$$$$$$$$ (mapcar-append '(0) '((1 2) (1) (2)))
((0 1 2) (0 1) (0 2))
_$$$$$$$$ (setpower '(0) '((1 2) (1) (2)))
((0 1 2) (0 1) (0 2) (1 2) (1) (2))
|;
(defun setpower (lsta lstb)
(defun mapcar-append (lsta lstb)
(if (null lsta)
nil
(if (null lstb)
nil
(cons (append lsta (car lstb)) (mapcar-append lsta (cdr lstb)))
)
)
)
(append (mapcar-append lsta lstb) lstb)
;;(append (mapcar '(lambda (x) (append lsta x)) lstb) lstb)
)
lanjqka
发表于 2014-1-16 16:21:32
;|
_$$ (find-matching-pair '= '(1 2 3 4 5 6 7 1))
(1 . 1)
_$$ (find-matching-pair '< '(1 2 3 4 5 6 7 1))
(1 . 2)
_$$ (find-matching-pair '> '(1 2 3 4 5 6 7 1))
nil
|;
(defun find-matching-pair (test lst)
(if (null (cadr lst))
nil
(if ((eval test) (car lst) (cadr lst))
(cons (car lst) (cadr lst))
(find-matching-pair test (cons (car lst) (cddr lst)))
)
)
)
q3_2006
发表于 2014-1-17 11:37:14
这个用递归怎么写...拜托高手指导下(defun tst (l n / k nn)
(setq k -1 nn (length l))
(mapcar
(function
(lambda (a / ll m)
(setq k (1+ k) m k)
(repeat n
(setq ll (append ll (list (nth m l))))
(setq m (rem (1+ m) nn))
ll
)
)
)
l
)
)
lanjqka
发表于 2014-1-20 10:34:51
q3_2006 发表于 2014-1-17 11:37 static/image/common/back.gif
这个用递归怎么写...拜托高手指导下
;|
_$$ (ntha 2 '(0 1 2 3 4))
(0 1)
|;
(defun ntha (n lst)
(if (null lst)
nil
(if (zerop n)
nil
(cons (car lst) (ntha (- n 1) (cdr lst)))
)
)
)
;|
_$$ (tst '(0 1 2 3 4) 2)
((0 1) (1 2) (2 3) (3 4))
|;
(defun tst (lst n)
(if (null lst)
nil
(if (< (length lst) n)
nil
(cons (ntha n lst) (tst (cdr lst) n))
)
)
)
lanjqka
发表于 2014-1-20 10:36:11
;|
_$$ (-sublst '(0 1 2 3 4 5 6) 2 3)
(2 3 4)
|;
(defun -sublst (lst i n) ;(if (catchapply (sublst lst ist iend)
(if (null lst)
nil
(if (zerop i)
(if (zerop n)
nil
(cons (car lst) (-sublst (cdr lst) i (- n 1)))
)
(-sublst (cdr lst) (- i 1) n)
)
)
)
;|
_$$ (nthrm 2 '(0 1 2 3 4))
(0 1 3 4)
_$$ (nthrm 0 '("a"))
nil
|;
(defun nthrm (n lst)
(if (null lst)
nil
(if (zerop n)
(cdr lst)
(cons (car lst) (nthrm (- n 1) (cdr lst)))
)
)
)
;|
_$$ (ntha 2 '(0 1 2 3 4))
(0 1)
|;
(defun ntha (n lst)
(if (null lst)
nil
(if (zerop n)
nil
(cons (car lst) (ntha (- n 1) (cdr lst)))
)
)
)
;|
_$$ (nthb 2 '(0 1 2 3 4))
(3 4)
|;
(defun nthb (n lst)
(if (null lst)
nil
(if (zerop n)
(cdr lst)
(nthb (- n 1) (cdr lst))
)
)
)
;|
_$$ (nthac 2 '(0 1 2 3 4))
(0 1 2)
|;
(defun nthac (n lst)
(if (null lst)
nil
(if (zerop n)
(cons (car lst) nil)
(cons (car lst) (nthac (- n 1) (cdr lst)))
)
)
)
;|
_$$ (nthbc 2 '(0 1 2 3 4))
(2 3 4)
|;
(defun nthbc (n lst)
(if (null lst)
nil
(if (zerop n)
lst
(nthbc (- n 1) (cdr lst))
)
)
)
q3_2006
发表于 2014-1-20 12:36:43
lanjqka 发表于 2014-1-20 10:36 static/image/common/back.gif
谢谢,学习了!
lanjqka
发表于 2014-1-20 13:35:36
;|
_$$ (-sub-cons-s '(0 1 2 3 4 5 6) 2)
((0 1) (2 3) (4 5))
|;
(defun -sub-cons-s (lst n)
(if (null lst)
nil
(if (< (length lst) n)
nil
(cons (ntha n lst) (-sub-cons-s (nthbc n lst) n)))))