@pbejse wrote:
....
is it just so happen that the list is sorted that way? will it be ever like this '("A" 2 1.5 "A") ? where the order or type has nothing to do with the result? ....
Despite the heading on that linked thread, Lee Mac's (nCr) routine works with repeat items in the list, and with different kinds of items. So, given your example list:
(setq lst '("A" 2 1.5 "A"))
Then we do:
Command: (setq result (list lst))
(("A" 2 1.5 "A")) ; initial result list containing original list [last item in final result]
Command: (repeat (setq m (- (length lst) 2)) (setq result (append (nCr lst (1+ m)) result)) (setq m (1- m)))
0 [what it returns to the Command line at the end, but what it has built is:]
Command: !result
(("A" 2) ("A" 1.5) ("A" "A") (2 1.5) (2 "A") (1.5 "A") ("A" 2 1.5) ("A" 2 "A") ("A" 1.5 "A") (2 1.5 "A") ("A" 2 1.5 "A"))
EDIT: Or, to put it together:
(defun AllSubSets (lst / nCr m)
;; (nCr) function by Lee Mac on "Combinations without
;; repetition 3 and 4 element." thread, February 9, 2016
(defun nCr (l r)
(cond
((< r 2)
(mapcar 'list l)
)
(l
(append
(mapcar '(lambda ( x ) (cons (car l) x)) (nCr (cdr l) (1- r)))
(nCr (cdr l) r)
)
)
)
)
(setq result (list lst))
(repeat (setq m (- (length lst) 2))
(setq result ; not localized
(append
(nCr lst (1+ m))
result
); append
); setq
(setq m (1- m))
); repeat
result ; reported to Command line [also remains in variable]
); defun
Usage:
Command: (AllSubSets '("A" 2 1.5 "A"))
(("A" 2) ("A" 1.5) ("A" "A") (2 1.5) (2 "A") (1.5 "A") ("A" 2 1.5) ("A" 2 "A") ("A" 1.5 "A") (2 1.5 "A") ("A" 2 1.5 "A"))
Kent Cooper, AIA