Author Topic: ----<<Challenge>>----atom to nested list  (Read 1496 times)

0 Members and 1 Guest are viewing this topic.

Jeremy Dunn

  • Newt
  • Posts: 31
----<<Challenge>>----atom to nested list
« on: November 17, 2020, 03:30:57 PM »
I am searching for a function that will take an atom and substitute it for the atoms in a supplied list or just return the atom if the supplied list is an atom itself.
Some examples:

(atom2lst 3 5) -> 3
(atom2lst 2 '(3 5)) -> (2 2)
(atom2lst  2 ((1 2)((3 4) 5))) -> ((2 2)((2 2) 2))

d2010

  • Bull Frog
  • Posts: 326
Re: ----<<Challenge>>----atom to nested list
« Reply #1 on: November 17, 2020, 03:56:06 PM »
He say her son like chocolate.
Ask her if Son likes choco.
Code: [Select]
Command: (defun q2 (a1 / $rr 2) (setq $rr 2)) ?> Q2
Command: (mapcar 'q2 '(1 2 3 4)) ?> (2 2 2 2)
Command: (mapcar 'q2 '((1 2)((3 4) 5))) ?> (2 2)
Command: (mapcar 'q2 '((1 2)((3 4) 5))) ?>(2 2)
Command: (subst  *  2 '(1 2 3)) ?>(1 #<SUBR @06ebc588 *> 3)
« Last Edit: November 17, 2020, 05:42:09 PM by d2010 »

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: ----<<Challenge>>----atom to nested list
« Reply #2 on: November 17, 2020, 04:50:40 PM »
No dotted pairs:
Code - Auto/Visual Lisp: [Select]
  1. (defun atom2lst ( a b )
  2.     (if (listp b) (mapcar '(lambda ( x ) (atom2lst a x)) b) a)
  3. )

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: ----<<Challenge>>----atom to nested list
« Reply #3 on: November 17, 2020, 04:55:26 PM »
To cater for dotted pairs:
Code - Auto/Visual Lisp: [Select]
  1. (defun atom2lst ( a b )
  2.     (cond
  3.         (   (null b) nil)
  4.         (   (atom b) a)
  5.         (   (cons (atom2lst a (car b)) (atom2lst a (cdr b))))
  6.     )
  7. )
Code - Auto/Visual Lisp: [Select]
  1. _$ (atom2lst 2 '(3 . 5))
  2. (2 . 2)
  3. _$ (atom2lst 2 '(3 5))
  4. (2 2)
« Last Edit: November 17, 2020, 06:34:57 PM by Lee Mac »

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: ----<<Challenge>>----atom to nested list
« Reply #4 on: November 17, 2020, 06:22:43 PM »
If Lee’s posts had a love child (untested - coded blind on iPad - not yet able to sit at a computer) ... should be able to process regular lists as well as dotted pairs:

Code: [Select]
(defun atl (a x)
    (cond
        ((null x) nil)
        ((atom x) a)
        ((vl-list-length x) (mapcar '(lambda (l) (atl a l)) x))
        ((cons (atl a (car x)) (atl a (cdr x))))
    )
)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com • http://cadanalyst.slack.com • http://linkedin.com/in/cadanalyst

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: ----<<Challenge>>----atom to nested list
« Reply #5 on: November 17, 2020, 06:34:06 PM »
Nice one MP, though my second function will already process standard lists too  :wink:

Hope you're on the mend!

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: ----<<Challenge>>----atom to nested list
« Reply #6 on: November 17, 2020, 06:35:44 PM »
Well that’s what I get for coding blind. :lol: I’m

Getting a little better each day - just like all of us. :)

Thanks Lee.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com • http://cadanalyst.slack.com • http://linkedin.com/in/cadanalyst