+ Reply to Thread
Page 1 of 3 1 2 3 LastLast
Results 1 to 10 of 24
  1. #1
    Full Member
    Using
    AutoCAD 2006
    Join Date
    Aug 2012
    Location
    La Coruña. España
    Posts
    54

    Default convert list with mapcar, lambda...

    Registered forum members do not see this ad.

    I need to convert a list:
    (51 41 83 10 60 32 46 84)

    to list:
    ((51 41)(83 10)(60 32)(46 84))

    Regards

  2. #2
    Senior Member marko_ribar's Avatar
    Computer Details
    marko_ribar's Computer Details
    Operating System:
    Windows 7 Ultimate X64
    Computer:
    Intel quad core CPU 4x2.66GHz, 8GB RAM
    Motherboard:
    INTEL compatibile
    CPU:
    quad core 4x2.66GHz
    RAM:
    8GB
    Graphics:
    NVIDIA GeForce 6600 GT
    Primary Storage:
    250 GB
    Secondary Storage:
    500 GB
    Monitor:
    Samsung 17''
    Discipline
    Architectural
    marko_ribar's Discipline Details
    Occupation
    Architecture, project designer, project visualisation
    Discipline
    Architectural
    Details
    space design - modeling and animations
    Using
    AutoCAD 2012
    Join Date
    Feb 2010
    Location
    Belgrade, Serbia, Europe
    Posts
    332

    Default

    Code:
    (defun every2nd ( lst )
      (vl-remove nil (mapcar '(lambda ( a ) (if (eq (rem (vl-position a lst) 2) 0) a)) lst))
    )
    
    (defun groupby2 ( lst )
      (mapcar '(lambda ( a b ) (list a b)) (every2nd lst) (every2nd (cdr lst)))
    )
    
    (groupby2 '(51 41 83 10 60 32 46 84))
    M.R.

    Marko Ribar, d.i.a. (graduated engineer of architecture)
    M.R. on YouTube

  3. #3
    Super Member David Bethel's Avatar
    Discipline
    Multi-disciplinary
    David Bethel's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Commercial Food Service
    Using
    AutoCAD pre 2000
    Join Date
    Dec 2003
    Location
    Newport News, Virginia
    Posts
    1,926

    Default

    Maybe :



    Code:
    (setq lst '(51 41 83 10 60 32 46 84))
    
    (defun lby2 (l / tmp)
      (while l
        (setq tmp (cons (list (nth 0 l) (nth 1 l)) tmp)
                l (cdr l)
                l (cdr l)))
     (reverse tmp))

    I would recommend doing some basic error checking for these types of routine

    ie is the length of the list an even number.

    -David
    R12 (Dos) - A2K

  4. #4
    Forum Newbie
    Using
    AutoCAD 2008
    Join Date
    Jun 2012
    Posts
    3

    Default

    Code:
    (defun every2 (l)
      (cond ((and (car l) (cadr l))
      (cons (list (car l) (cadr l)) (every2 (cddr l)))
     )
     ((car l) (list l))
      )
    )

  5. #5
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Windows 7 Ultimate (32-bit)
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2013
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    15,738

    Default

    Code:
    (defun g2 ( l ) (if l (cons (list (car l) (cadr l)) (g2 (cddr l)))))
    Lee Mac Programming

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

    Just another Swamper

  6. #6
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Windows 7 Ultimate (32-bit)
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2013
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    15,738

    Default

    Quote Originally Posted by marko_ribar View Post
    Code:
    (defun every2nd ( lst )
      (vl-remove nil (mapcar '(lambda ( a ) (if (eq (rem (vl-position a lst) 2) 0) a)) lst))
    )
    
    (defun groupby2 ( lst )
      (mapcar '(lambda ( a b ) (list a b)) (every2nd lst) (every2nd (cdr lst)))
    )
    
    (groupby2 '(51 41 83 10 60 32 46 84))
    Be careful with duplicates...
    Code:
    _$ (every2nd '(1 2 3 1 2 3))
    (1 3 1 3)
    For your method, consider perhaps:
    Code:
    (defun f ( l / i )
        (setq i -1)
        (vl-remove-if '(lambda ( x ) (= 1 (rem (setq i (1+ i)) 2))) l)
    )
    (defun g2 ( l ) (mapcar 'list (f l) (f (cdr l))))
    Code:
    _$ (g2 '(1 2 3 1 2 3))
    ((1 2) (3 1) (2 3))
    Lee Mac Programming

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

    Just another Swamper

  7. #7
    Senior Member
    Discipline
    Structural
    Using
    AutoCAD 2011
    Join Date
    Sep 2011
    Location
    Baia Mare, Romania
    Posts
    136

    Default

    Three alternatives
    1 - recursive - similar to Lee (see l2p function)

    2 - using foreach
    Code:
    (defun ph1:l2p (l / s r)
      (foreach x l
        (if s
           (setq r (cons (list s x) r)
                 s nil
           )
           (setq s x)
        )
      )
      (reverse r)
    )
    3 - using repeat
    Code:
    (defun ph2:l2p (l / r)
      (repeat (/ (length l) 2)
        (setq r (append r (list (list (car l) (cadr l))))
              l (cddr l)
        )
      )
      r
    )

  8. #8
    Full Member
    Using
    AutoCAD 2006
    Join Date
    Aug 2012
    Location
    La Coruña. España
    Posts
    54

    Default

    Uffff. I will explore all options, to see which one i choose.
    I thought it would be easier with mapcar and lambda.
    Thank you all.
    Regards.

  9. #9
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Windows 7 Ultimate (32-bit)
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2013
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    15,738

    Default

    Quote Originally Posted by robierzo View Post
    I thought it would be easier with mapcar and lambda.
    Note that not all tasks are best solved using mapcar.
    For this particular example, the items in the list need to be processed in pairs, hence mapcar is not best suited for the task since it will process every item in a list consecutively, meaning that you must first manipulate the list before passing it to mapcar. This extra work to manipulate the list is very inefficient where other methods could process the list directly.

    The mapcar function is just one tool in your AutoLISP toolbox, from which you should choose the best tool for the job. Restricting yourself to solely using mapcar for every task would be like building a house with just a hammer...
    Lee Mac Programming

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

    Just another Swamper

  10. #10
    Full Member
    Using
    AutoCAD 2006
    Join Date
    Aug 2012
    Location
    La Coruña. España
    Posts
    54

    Default

    Registered forum members do not see this ad.

    O.K. making way....Thanks

Similar Threads

  1. Mapcar and Lambda Exercises
    By Bill Tillman in forum AutoLISP, Visual LISP & DCL
    Replies: 13
    Last Post: 10th Apr 2012, 04:09 pm
  2. MAPCAR-LAMBDA problem
    By kruuger in forum AutoLISP, Visual LISP & DCL
    Replies: 3
    Last Post: 5th Feb 2011, 02:42 pm
  3. Mapcar + lambda Description
    By Sweety in forum AutoLISP, Visual LISP & DCL
    Replies: 22
    Last Post: 11th Sep 2010, 02:14 pm
  4. mapcar + lambda
    By Michaels in forum AutoLISP, Visual LISP & DCL
    Replies: 1
    Last Post: 16th Jul 2010, 04:57 pm
  5. lambda, mapcar in lisp
    By vivekgrs in forum AutoLISP, Visual LISP & DCL
    Replies: 2
    Last Post: 7th Aug 2006, 12:58 am

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts