Author Topic: [challenge] A11 : Duplicate the elements of a list  (Read 262 times)

0 Members and 1 Guest are viewing this topic.

JohnK

  • Administrator
  • Needs a day job
  • Posts: 9947
[challenge] A11 : Duplicate the elements of a list
« on: January 10, 2022, 09:06:20 AM »
Using only AutoLisp, duplicate the elements of a list.
Example:
(dupli '(a b c c d))
> (A A B B C C C C D D)
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

VovKa

  • Swamp Rat
  • Posts: 1417
  • Ukraine
Re: [challenge] A11 : Duplicate the elements of a list
« Reply #1 on: January 10, 2022, 09:19:32 AM »
Code: [Select]
(defun f (l)
  (if l (cons (car l) (cons (car l) (f (cdr l)))))
)
(defun f2 (l)
  (apply 'append (mapcar 'list l l))
)
« Last Edit: January 10, 2022, 09:23:54 AM by VovKa »

dexus

  • Newt
  • Posts: 27
Re: [challenge] A11 : Duplicate the elements of a list
« Reply #2 on: January 10, 2022, 09:23:55 AM »
First thing that comes to mind:
Code - Auto/Visual Lisp: [Select]
  1. (defun dupli-dexus (l / r)
  2.   (while l
  3.     (setq r (cons (car l) (cons (car l) r))
  4.           l (cdr l))
  5.   )
  6.   (reverse r)
  7. )

JohnK

  • Administrator
  • Needs a day job
  • Posts: 9947
Re: [challenge] A11 : Duplicate the elements of a list
« Reply #3 on: January 10, 2022, 09:37:48 AM »
Here was mine (is not that efficient):
Code - Auto/Visual Lisp: [Select]
  1. (defun dupli-se7en (aList)
  2.   (apply
  3.     'append
  4.     (mapcar
  5.       '(lambda (x)
  6.          (list x x))
  7.       aList)
  8.     )
  9.   )
  10.  
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

kirby

  • Newt
  • Posts: 57
Re: [challenge] A11 : Duplicate the elements of a list
« Reply #4 on: January 10, 2022, 10:28:27 AM »
Verbose and without any of that mapcar / lambda obfuscation!

Code: [Select]
(defun Duplicate (MyList / OutList CNT MyItem1)
; A11 : Duplicate the elements of a list
; e.g. (duplicate '(a b c c d))
; output (A A B B C C C C D D)

(setq OutList nil)

(if (eq (type MyList) 'LIST)
  (progn
(setq CNT 0)
(repeat (length MyList)
(setq MyItem (nth CNT MyList))

(repeat 2
(setq OutList (cons MyItem OutList))
)

(setq CNT (1+ CNT))
) ; close repeat

(setq OutList (reverse OutList))
  )
) ; close if
OutList
)

gile

  • Water Moccasin
  • Posts: 2340
  • Marseille, France
Re: [challenge] A11 : Duplicate the elements of a list
« Reply #5 on: January 10, 2022, 10:52:28 AM »
Code: [Select]
(defun f (l)
  (if l (cons (car l) (cons (car l) (f (cdr l)))))
)
(defun f2 (l)
  (apply 'append (mapcar 'list l l))
)
What more can be said?
Speaking English as a French Frog

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1254
  • Marco
Re: [challenge] A11 : Duplicate the elements of a list
« Reply #6 on: January 10, 2022, 11:02:49 AM »
Foreach version:
Code: [Select]
(defun ALE_Dupli (L / o)
  (reverse
    (foreach f L
      (setq o (cons f (cons f o)))
    )
  )
)

JohnK

  • Administrator
  • Needs a day job
  • Posts: 9947
Re: [challenge] A11 : Duplicate the elements of a list
« Reply #7 on: January 10, 2022, 11:23:03 AM »
What more can be said?

Not much, honestly, but that would have been a very short challenge if we payed attention to VovKa. ;P
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

VovKa

  • Swamp Rat
  • Posts: 1417
  • Ukraine
Re: [challenge] A11 : Duplicate the elements of a list
« Reply #8 on: January 10, 2022, 12:15:06 PM »
if we payed attention to VovKa. ;P
i'm still h e r e  :knuppel2:

JohnK

  • Administrator
  • Needs a day job
  • Posts: 9947
Re: [challenge] A11 : Duplicate the elements of a list
« Reply #9 on: January 10, 2022, 12:26:54 PM »
lol
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

BIGAL

  • Swamp Rat
  • Posts: 897
  • 30 + years of using Autocad
Re: [challenge] A11 : Duplicate the elements of a list
« Reply #10 on: January 10, 2022, 08:08:47 PM »
Where is (dupli '(a b c c d) num) it should be obvious about num. 2 has been done.
A man who never made a mistake never made anything

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1254
  • Marco
Re: [challenge] A11 : Duplicate the elements of a list
« Reply #11 on: January 11, 2022, 03:08:21 AM »
Code: [Select]
(defun ALE_DupliN (L n / o)
  (reverse
    (foreach f L
      (setq o (cons f o))
      (repeat n
        (setq o (cons f o))
      )
      o
    )
  )
)

(ALE_DupliN '() 2)          => nil
(ALE_DupliN '(a b c c d) 0) => (A B C C D)
(ALE_DupliN '(a b c c d) 1) => (A A B B C C C C D D)
(ALE_DupliN '(a b c c d) 2) => (A A A B B B C C C C C C D D D)

dexus

  • Newt
  • Posts: 27
Re: [challenge] A11 : Duplicate the elements of a list
« Reply #12 on: January 11, 2022, 03:17:07 AM »
Where is (dupli '(a b c c d) num) it should be obvious about num. 2 has been done.
Ah, you mean challenge A12 :wink:
Definitely would make the function more versatile.
Code - Auto/Visual Lisp: [Select]
  1. (defun dupli-dexus-x (l x / r)
  2.   (while l
  3.     (repeat x
  4.       (setq r (cons (car l) r))
  5.     )
  6.     (setq l (cdr l))
  7.   )
  8.   (reverse r)
  9. )


Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1254
  • Marco
Re: [challenge] A11 : Duplicate the elements of a list
« Reply #13 on: January 11, 2022, 04:55:15 AM »
Where is (dupli '(a b c c d) num) it should be obvious about num. 2 has been done.
Ah, you mean challenge A12 ;)
Definitely would make the function more versatile.
Code - Auto/Visual Lisp: [Select]
  1. (defun dupli-dexus-x (l x / r)
  2.   (while l
  3.     (repeat x
  4.       (setq r (cons (car l) r))
  5.     )
  6.     (setq l (cdr l))
  7.   )
  8.   (reverse r)
  9. )

(dupli-dexus-x '(a b c c d) 0) => nil  IMHO it is better => (A B C C D)

dexus

  • Newt
  • Posts: 27
Re: [challenge] A11 : Duplicate the elements of a list
« Reply #14 on: January 12, 2022, 09:49:27 AM »
[...]

(dupli-dexus-x '(a b c c d) 0) => nil  IMHO it is better => (A B C C D)

I agree, it does make sense when zero adds nothing to the list.
Although if you see the second variable as the amount of times the item should be in the list, then 0 would return an empty list or nil. This would also make sense, so I'm not sure which would be 'better'.

So amount of times vs amount of times added.
« Last Edit: January 12, 2022, 09:52:40 AM by dexus »