学习 lisp 递归
本帖最后由 lanjqka 于 2014-1-11 11:25 编辑还是递归 继续递归
(defun str2lst (str / n)
(setq str (vl-string-trim " " str))
(setq n (vl-string-search " " str))
(if (= str "")
nil
(cons (substr str 1 n) (if n (str2lst (substr str (+ n 1))))
)
)
) ;同vl-remove(defun ouremove (x l)
(if (null l)
nil
(if (equal x (car l))
(ouremove x (cdr l))
(cons (car l) (ouremove x (cdr l)))
)
)
) 这学习精神值得我们学习 这个是好东西!1 本帖最后由 lanjqka 于 2014-1-13 16:57 编辑
;;同vl-position(defun ourposition (u l / i)
(if (null l)
nil
(if (eq u (car l))
0
(progn (setq i (ourposition u (cdr l))) (setq i (+ i 1)))
)
)
);;再改(defun ourposition (a l)
(if (null l)
nil
(if (equal a (car l))
0
(+ (ourposition a (cdr l)) 1)
)
)
) http://bbs.mjtd.com/thread-108956-1-1.html
;;vl-string->list(defun string->list (str)
(if (zerop (ascii str))
nil
(cons (ascii str) (string->list (substr str 2)))
)
);;(defun reverstr (str) ;倒置字符串
(if (zerop (ascii str))
""
(if (<= (ascii str) 128)
(strcat (reverstr (substr str 2)) (substr str 1 1))
(strcat (reverstr (substr str 3)) (substr str 1 2))
)
)
)
http://bbs.mjtd.com/thread-108944-1-1.html
看了网上帖子,gcd是用了rem做辗转相除的,那可能是有效的。(defun gcd (a b)
(if (= b 0)
a
(gcd b (rem a b))))来个 辗转相减(defun gcd2 (a b)
(if (= a b)
a
(gcd2 b (abs (- a b)))
)
);;数表的最大公约数(defun ourgcd (l)
(if (null (cadr l))
(car l)
(ourgcd (cons (gcd (car l) (cadr l)) (cddr l)))
)
) 本帖最后由 lanjqka 于 2014-1-11 03:37 编辑
最近学习了递归 没有收住 欢迎大家一起来
递归的感觉
(...
(if (退出条件)
退出形式
(符合递归形式)
)
...
) 本帖最后由 lanjqka 于 2014-1-11 04:02 编辑
;; getnum
(defun ourgetnum (/ val)
(setq val (read (getstring "\nPlease enter a number : ")))
(if (numberp val)
(eval val)
(ourgetnum)
)
) ;;转录 ;;同length(defun ourlength (l)
(if (null l)
0
(+ (ourlength (cdr l)) 1)
)
)
;;同member(defun ourmember (x l)
(if (null l)
nil
(if (equal x (car l))
l
(ourmember x (cdr l))
)
)
) 本帖最后由 lanjqka 于 2014-1-13 13:09 编辑
;; nth
(defun ournth (n l)
(if (null l)
nil
(if (zerop n)
(car l)
(ournth (- n 1) (cdr l))
)
)
)