October 12th Friday (十月 十二日 金曜日
2007-11-18 18:51
381 查看
How to work out the enclosure sets of a set? I implemented it in Emacs-lisp. The source is as follow.
(setq empty-set (list nil))
(defun remove-dup (l)
(defun rm-dup (ls rs)
(cond ((null ls) rs)
((not (in-set (car ls) (cdr ls)))
(rm-dup (cdr ls) (cons (car ls) rs)))
(t (rm-dup (cdr ls) rs))))
(rm-dup l nil))
(defun removed-one-subsets (s)
(mapcar (lambda (e)
(remove e s)) s))
(defun removed-one-subsets-from-sets (ss)
(let ((rs nil))
(while (not (null ss))
(mapcar (lambda (x)
(setq rs (cons x rs)))
(removed-one-subsets (car ss)))
(setq ss (cdr ss)))
(remove-dup rs)))
(defun enclosure-sets (set)
(let ((n (length set))
(rs (list set))
(ss (removed-one-subsets set)))
(setq rs (append rs ss))
(while (> n (- (length set) 2))
(setq ss (removed-one-subsets-from-sets ss))
(setq rs (append rs ss))
(setq n (- n 1)))
(remove-dup (append rs empty-set))))
(enclosure-sets (list 1 2 3))
=> (nil (1) (2) (3) (1 2) (1 3) (2 3) (1 2 3))
The codes is not beautiful. I discussed about the problem with my a workmates. He thinks it easy to
implement. But it does not seem to be easy in reality. If try to combine ones that is extracted from the
original set, you have to met lots of problems, such as how to get one or more elements from a set and how to
combine more elements. So, I adapted another way to remove one or more elements from the original set for
making a new sub-set. The sub-set can also repeat deleting its elements until there are all one element sets.
What is more, generally, a set doesn't include duplicated elements. However, during working out the
enclosure-sets, there are some temporary sets or set have duplicated elements. So, I wrote the function
remove-dup to make a new set or list from the original set or list that have not duplicated elements.
Finally, don't forget appending a empty-set and the original set into the enclosure-set.
(setq empty-set (list nil))
(defun remove-dup (l)
(defun rm-dup (ls rs)
(cond ((null ls) rs)
((not (in-set (car ls) (cdr ls)))
(rm-dup (cdr ls) (cons (car ls) rs)))
(t (rm-dup (cdr ls) rs))))
(rm-dup l nil))
(defun removed-one-subsets (s)
(mapcar (lambda (e)
(remove e s)) s))
(defun removed-one-subsets-from-sets (ss)
(let ((rs nil))
(while (not (null ss))
(mapcar (lambda (x)
(setq rs (cons x rs)))
(removed-one-subsets (car ss)))
(setq ss (cdr ss)))
(remove-dup rs)))
(defun enclosure-sets (set)
(let ((n (length set))
(rs (list set))
(ss (removed-one-subsets set)))
(setq rs (append rs ss))
(while (> n (- (length set) 2))
(setq ss (removed-one-subsets-from-sets ss))
(setq rs (append rs ss))
(setq n (- n 1)))
(remove-dup (append rs empty-set))))
(enclosure-sets (list 1 2 3))
=> (nil (1) (2) (3) (1 2) (1 3) (2 3) (1 2 3))
The codes is not beautiful. I discussed about the problem with my a workmates. He thinks it easy to
implement. But it does not seem to be easy in reality. If try to combine ones that is extracted from the
original set, you have to met lots of problems, such as how to get one or more elements from a set and how to
combine more elements. So, I adapted another way to remove one or more elements from the original set for
making a new sub-set. The sub-set can also repeat deleting its elements until there are all one element sets.
What is more, generally, a set doesn't include duplicated elements. However, during working out the
enclosure-sets, there are some temporary sets or set have duplicated elements. So, I wrote the function
remove-dup to make a new set or list from the original set or list that have not duplicated elements.
Finally, don't forget appending a empty-set and the original set into the enclosure-set.
相关文章推荐
- October 26th Friday (十月 二十六日 金曜日)
- 2008 October 17th Friday (十月 十七日 金曜日)
- 2008 October 10th Friday (十月 十日 金曜日)
- June 12th Friday (六月 十二日 金曜日)
- October 19th Friday (十月 十九日 金曜日)
- 2008 September 12th Friday (九月 十二日 金曜日)
- 2008 October 24th Friday (十月 二十四日 金曜日)
- 2008 October 31th Friday (十月 三十一日 金曜日)
- May 18th Friday (五月 十八日 金曜日)
- August 24th Friday (八月 二十四日 金曜日)
- September 12th Wednesday (九月 十二日 水曜日
- October 17th Wednesday (十月 十七日 水曜日)
- December 14th Friday (十二月 十四日 金曜日)
- 2008 February 1st Friday (二月 一日 金曜日)
- 2008 April 18th Friday (四月 十八日 金曜日)
- 2008 July 11th Friday (七月 十一日 金曜日)
- 2008 August 22th Friday (八月 二十二日 金曜日)
- 2008 October 8th Wednesday (十月 八日 水曜日)
- 2008 October 29th Wednesday (十月 二十九日 水曜日)
- January 23th Friday 2009 (一月 二十三日 金曜日)