明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
楼主: lanjqka

[讨论] 学习 lisp 递归

[复制链接]
发表于 2014-5-5 11:29 | 显示全部楼层
本帖最后由 q3_2006 于 2014-5-6 19:00 编辑
lanjqka 发表于 2014-1-11 12:46
;;ourappend;;ourlist


明经群里别人的问题

'((6 7) (4 "A") (8 "B") (1 2) (3 4) (6 5) (2 3) ("A" 9) ("B" 10) (7 8))
->
(((6 7) (7 8) (6 5) (8 "B") ("B" 10)) ((4 "A") ("A" 9) (1 2) (2 3) (3 4)))

只要x或y有相等的情况就分为同一组...
感觉用递归写比较方便...但我还是写不好...拜托大师抽空指点下...谢谢了!
 楼主| 发表于 2014-5-26 20:27 | 显示全部楼层
希望对你有所帮助
  1. ;|
  2. _$$ (-f1 '(6 7) '((4 "A") (8 "B") (1 2) (3 4) (6 5) (2 3) ("A" 9) ("B" 10) (7 8)))
  3. ((6 5) (7 8))
  4. |;
  5. (defun -f1 (at lst)
  6.   (if (null at)
  7.     nil
  8.     (if        (null lst)
  9.       nil
  10.       (if (or (equal (car at) (caar lst))
  11.               (equal (cadr at) (cadar lst))
  12.               (equal (car at) (cadar lst))
  13.               (equal (cadr at) (caar lst)))
  14.         (cons (car lst) (-f1 at (cdr lst)))
  15.         (-f1 at (cdr lst))))))
  16. (defun -f12 (at lst) (vl-remove-if-not '(lambda (x) (or (member (car at) x) (member (cadr at) x))) lst))
  17. ;|
  18. _$$ (-f2 '(6 7) '((4 "A") (8 "B") (1 2) (3 4) (6 5) (2 3) ("A" 9) ("B" 10) (7 8)))
  19. ((4 "A") (8 "B") (1 2) (3 4) (2 3) ("A" 9) ("B" 10))
  20. |;
  21. (defun -f2 (at lst)
  22.   (if (null at)
  23.     (list lst)
  24.     (if        (null lst)
  25.       nil
  26.       (if (or (equal (car at) (caar lst))
  27.               (equal (cadr at) (cadar lst))
  28.               (equal (car at) (cadar lst))
  29.               (equal (cadr at) (caar lst)))
  30.         (-f2 at (cdr lst))
  31.         (cons (car lst) (-f2 at (cdr lst)))))))
  32. (defun -f22 (at lst) (vl-remove-if '(lambda (x) (or (member (car at) x) (member (cadr at) x))) lst))
  33. ;|
  34. _$$ (-f3 '((6 7) (6 5) (7 8)) '((4 "A") (8 "B") (1 2) (3 4) (2 3) ("A" 9) ("B" 10)))
  35. ((8 "B"))
  36. |;
  37. (defun -f3 (atl lst)
  38.   (if (null atl)
  39.     nil
  40.     (if        (null (-f1 (car atl) lst))
  41.       (-f3 (cdr atl) (-f2 (car atl) lst))
  42.       (append (-f1 (car atl) lst) (-f3 (cdr atl) (-f2 (car atl) lst))))))
  43. (defun -f32 (atl lst) (apply 'append (mapcar '(lambda (x) (-f12 x lst)) atl)))
  44. ;|
  45. _$$ (-f4 '((6 7) (6 5) (7 8)) '((4 "A") (8 "B") (1 2) (3 4) (2 3) ("A" 9) ("B" 10)))
  46. ((4 "A") (1 2) (3 4) (2 3) ("A" 9) ("B" 10))
  47. |;
  48. (defun -f4 (atl lst)
  49.   (if (null atl)
  50.     (list lst)
  51.     (if        (null lst)
  52.       nil
  53.       (if (null (-f1 (car lst) atl))
  54.         (cons (car lst) (-f4 atl (cdr lst)))
  55.         (-f4 atl (cdr lst))))))
  56. (defun -f42 (atl lst / tmp ret)
  57.   (setq tmp (-f32 atl lst))
  58.   (while lst
  59.     (if (null (member (car lst) tmp))
  60.       (setq ret (append ret (list (car lst)))))
  61.     (setq lst (cdr lst)))
  62.   ret
  63.   )
  64. ;|
  65. _$$ (-f5 '((6 7)) '((4 "A") (8 "B") (1 2) (3 4) (6 5) (2 3) ("A" 9) ("B" 10) (7 8)))
  66. (((6 7) (6 5) (7 8) (8 "B") ("B" 10)) ((4 "A") (1 2) (3 4) (2 3) ("A" 9)))
  67. _$$ (-f5 '((4 "A")) '((1 2) (3 4) (2 3) ("A" 9)))
  68. (((4 "A") (3 4) ("A" 9) (2 3) (1 2)))
  69. |;
  70. (defun -f5 (l1 l2)
  71.   (if (null l2)
  72.     (list l1)
  73.     (if        (null (-f3 l1 l2))
  74.       (list l1 l2)
  75.       (-f5 (append l1 (-f3 l1 l2)) (-f4 l1 l2)))))
  76. (defun -f52  (l1 l2 / tmp)
  77.   (while (setq tmp (-f32 l1 l2))
  78.     (setq l2 (-f42 l1 l2)
  79.           l1 (append l1 tmp)))
  80.   (if (null l2)
  81.     (list l1)
  82.     (list l1 l2)))
  83. ;|
  84. _$$ (-f6 '((6 7) (4 "A") (8 "B") (1 2) (3 4) (6 5) (2 3) ("A" 9) ("B" 10) (7 8)))
  85. (((6 7) (6 5) (7 8) (8 "B") ("B" 10)) ((4 "A") (3 4) ("A" 9) (2 3) (1 2)))
  86. |;
  87. (defun -f6 (lst / tmp)
  88.   (if (null (cdr lst))
  89.     (list lst)
  90.     (progn (setq tmp (-f5 (list (car lst)) (cdr lst)))
  91.            (if (null (cdr tmp))
  92.              tmp
  93.              (append (list (car tmp)) (-f6 (cadr tmp)))))))
  94. (defun -f62  (lst / tmp ret)
  95.   (while (and lst (setq tmp (-f52 (list (car lst)) (cdr lst))))
  96.     (setq ret (append ret (list (car tmp)))
  97.           lst (cadr tmp)))
  98.   ret)
发表于 2014-9-6 18:10 | 显示全部楼层
活络,看着神奇,也只能挑些来用了。
发表于 2022-11-29 11:26 | 显示全部楼层
这个是好东西!1
发表于 2022-11-29 11:28 | 显示全部楼层
谢谢,楼主分享!!
发表于 2024-4-21 18:20 | 显示全部楼层
这学习精神  值得我们学习
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-5-5 19:18 , Processed in 5.622847 second(s), 18 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表