Author Topic: I need a help to move from lists to one list  (Read 1689 times)

0 Members and 1 Guest are viewing this topic.

Coder

  • Swamp Rat
  • Posts: 827
I need a help to move from lists to one list
« on: June 19, 2014, 08:15:31 AM »
Hello guys .

I have a list of lists as it is posted below and I need to a code to make them as one list and remove nil .

Code: [Select]
(setq my-list '(("Two-Site layout.dwg" "Two - PLOT LIMIT.dwg")
            ("Three-Site layout.dwg")
            nil
            "one -Site layout.dwg"
            "one -PLOT LIMIT.dwg"
            nil
            (("Sub-2 -Site layout.dwg" "Sub-1 -Site layout.dwg") "Four -Site layout.dwg")
           )
)

is it possible ?

Many thanks in advance  :-)

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1454
  • Marco
Re: I need a help to move from lists to one list
« Reply #1 on: June 19, 2014, 08:48:01 AM »
Code: [Select]
(ALE_List_Flatten my-list)
=>> ("Two-Site layout.dwg" "Two - PLOT LIMIT.dwg" "Three-Site layout.dwg"
          "one -Site layout.dwg" "one -PLOT LIMIT.dwg" "Sub-2 -Site layout.dwg"
           "Sub-1 -Site layout.dwg" "Four -Site layout.dwg"
        )
choose the one you like:
Code: [Select]
; Marc'Antonio Alessi - http://xoomer.virgilio.it/alessi
; Function: ALE_List_Flatten - 11/03/2006
;
; Version 1.02 - 15/03/2006
;
; Description: Return a flat list from any kind of list
;
; Arguments: In_Lst = list
;
; Return Values: List or nil if In_Lst is nil
;
; Notes: remove all occurences of nil from result
;
; Example:
;   (setq alist '(0 (1 . 2) (3 nil 3) (5 (3 (6 (1 2) 3) 3 7))))
;
;   (ALE_List_Flatten alist)
;   Returns: (0 1 2 3 3 5 3 6 1 2 3 3 7)
;
(defun ALE_List_Flatten (In_Lst / OutLst)
  (reverse (ALE_List_Flatten_Aux In_Lst))
)
;
(defun ALE_List_Flatten_Aux (In_Lst)
  (foreach ForElm In_Lst
    (if (atom ForElm)
      (if ForElm (setq OutLst (cons ForElm OutLst)) OutLst)
      (if (listp (cdr ForElm))
        (ALE_List_Flatten_Aux ForElm)
        (setq OutLst (cons (cdr ForElm) (cons (car ForElm) OutLst)))
      )
    )
  )
)
;
; Notes: keep all occurences of nil:
; Returns:  (0 1 2 3 nil 3 5 3 6 1 2 3 3 7)
;
(defun ALE_List_Flatten_Aux (In_Lst)
  (foreach ForElm In_Lst
    (if (atom ForElm)
      (setq OutLst (cons ForElm OutLst))
      (if (listp (cdr ForElm))
        (ALE_List_Flatten_Aux ForElm)
        (setq OutLst (cons (cdr ForElm) (cons (car ForElm) OutLst)))
      )
    )
  )
)
;
; Marc'Antonio Alessi - http://xoomer.virgilio.it/alessi
;
; Function: ALE_List_Flatten_NR
; Version 1.00 - 15/03/2006
;
; Description: Return a flat list from any kind of list
;              Not recursive - slower
;
; Arguments: In_Lst = list
;
; Notes: keep all occurences of nil
;
(defun ALE_List_Flatten_NR (In_Lst / TmpVal TmpCar TmpCdr OutLst)
  (while In_Lst
    (if (atom (setq TmpVal (car In_Lst)))
      (setq OutLst (cons TmpVal OutLst) In_Lst (cdr In_Lst))
      (if (listp (setq TmpCar (car TmpVal)  TmpCdr (cdr TmpVal)))
        (if TmpCdr
          (setq In_Lst (cons TmpCar (cons TmpCdr (cdr In_Lst))))
          (setq In_Lst (cons TmpCar (cdr In_Lst)))
        )
        (setq
          OutLst (cons TmpCdr (cons TmpCar OutLst))
          In_Lst (cdr In_Lst)
        )
      )
    )
  )
  (reverse OutLst)
)
;
; Marc'Antonio Alessi - http://xoomer.virgilio.it/alessi
;
; Function: ALE_List_Flatten_N2
; Version 1.00 - 15/03/2006
;
; Description: Return a flat list from any kind of list
;              Not recursive - slower
;
; Arguments: In_Lst = list
;
; Notes: remove all occurences of nil from result
;
(defun ALE_List_Flatten_N2 (In_Lst / TmpVal TmpCar TmpCdr OutLst)
  (while In_Lst
    (if (atom (setq TmpVal (car In_Lst)))
      (progn
        (setq In_Lst (cdr In_Lst))
        (and TmpVal (setq OutLst (cons TmpVal OutLst)))
      )
      (if (listp (setq TmpCar (car TmpVal)  TmpCdr (cdr TmpVal)))
        (if TmpCdr
          (setq In_Lst (cons TmpCar (cons TmpCdr (cdr In_Lst))))
          (setq In_Lst (cons TmpCar (cdr In_Lst)))
        )
        (setq
          OutLst (cons TmpCdr (cons TmpCar OutLst))
          In_Lst (cdr In_Lst)
        )
      )
    )
  )
  (reverse OutLst)
)

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: I need a help to move from lists to one list
« Reply #2 on: June 19, 2014, 10:42:01 AM »
my version, for not dotted list:
Code - Auto/Visual Lisp: [Select]
  1. (defun f (l)
  2.   (cond ((not l) l)
  3.         ((atom l) (list l))
  4.         ((apply (function append) (mapcar (function f) l)))
  5.   )
  6. )

David Bethel

  • Swamp Rat
  • Posts: 656
Re: I need a help to move from lists to one list
« Reply #3 on: June 19, 2014, 10:56:31 AM »

From the Compuserve days :

Code: [Select]
;;;++++++++++++ Explode Any List Steve Johnson +++++++++++++++++++++
(defun exlist (LST / expl new)
  (defun expl (SUBLST)
    (foreach one SUBLST
      (if (atom one)
          (setq new (cons one new))
          (if (listp (cdr one))
              (expl one)
              (setq new (cons (cdr one) (cons (car one) new)))))))
  (expl (list LST))
  (reverse new))

-David
R12 Dos - A2K

Lee Mac

  • Seagull
  • Posts: 12929
  • London, England
Re: I need a help to move from lists to one list
« Reply #4 on: June 19, 2014, 01:00:02 PM »
Another couple, quite old now: Flatten List

Coder

  • Swamp Rat
  • Posts: 827
Re: I need a help to move from lists to one list
« Reply #5 on: June 19, 2014, 02:26:24 PM »
Many thanks for all guys , all work great  :-)

One question please , I read about the function atom but did not understand it well and I see all guys used this function .
Can anyone give a clear explanation about it please ?

Thanks again for all .