前几天,在院长群里看到院长说的一个问题,挺感兴趣,今天尝试写了一下,分享出来。
院长说的是
(aaa '("9" (("7" (("3" ("1" "2")) ("6" ("4" "5")))) ("8" (("3" ("1" "2")) ("6" ("4" "5")))))))
→
'(("9" "7" "3" "1")
("9" "7" "3" "2")
("9" "7" "6" "4")
("9" "7" "6" "5")
("9" "8" "3" "1")
("9" "8" "3" "2")
("9" "8" "6" "4")
("9" "8" "6" "5")
)
代码如下
- (defun LY:SplitBran(Var / first ret second second_1)
- (cond
- ((= (type Var) 'LIST)
- (setq first (car Var));第一个元素
- (if (= (type first) 'STR)
- (progn
- (setq second (cadr Var))
- (cond
- ((= (type second) 'list);第二个元素是表
- (progn
- (setq second_1
- (vl-remove nil (mapcar '(lambda (x)
- (cond
- ( (= (type x) 'STR)
- x
- )
- ((= (type x) 'list)
- (LY:SplitBran x)
- )
- (t
- nil
- )
- )
- )
- second
- )
- )
- );去除nil
- (setq second_1 (append second_1))
- (setq Ret '())
- (foreach X second_1
- (if (= (type X) 'list)
- (foreach Y X
- (setq Ret (cons Y Ret))
- )
- (setq Ret (cons (list X) Ret))
- )
- )
- (reverse (mapcar '(lambda (x)(cons first X)) Ret))
- )
- )
- ((= (type second) 'STR);第二个元素是字符
- ;直接原样返回
- (cdr Var)
- )
- (t;其他情况
- nil
- )
- )
- )
- (;第一个元素不是字符,就返回nil
- nil
- )
- )
- )
- (t ;如果不是表,就返回nil
- nil
- )
- )
- )
- (defun c:tt ()
- (setq var '("9" (("7" (("3" ("1" "2")) ("6" ("4" "5")))) ("8" (("3" ("1" "2")) ("6" ("4" "5")))))))
- (LY:SplitBran var)
- )
|