Author Topic: Challenge: Plaque ...  (Read 2096 times)

0 Members and 1 Guest are viewing this topic.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Challenge: Plaque ...
« on: April 28, 2005, 05:17:56 PM »
Challenge: Write a routine that inserts an arbitrary item between every item in a list. If the list contains only one item it should return the list unaltered.

Example:

(plaque '* '(tooth tooth tooth))

Should return (tooth * tooth * tooth)

(plaque '* '(tooth))

Should return (tooth)

My stabs ...

Code: [Select]
;; iterative using foreach

(defun plaque ( x lst / result )
    (cons (car lst)
        (foreach item (reverse (cdr lst))
            (setq result
                (cons x (cons item result))  
            )
        )
    )
)

;;  using mapcar

(defun plaque ( x lst )
    (cons (car lst)
        (apply 'append
            (mapcar
               '(lambda (item) (list x item))
                (cdr lst)
            )
        )
    )
)

;;  recursive

(defun plaque ( x lst / tail )
    (if lst
        (if (setq tail (cdr lst))
            (cons
                (car lst)
                (cons x (plaque x tail))    
            )
            lst
        )    
    )    
)

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

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Challenge: Plaque ...
« Reply #1 on: April 28, 2005, 05:25:58 PM »
Can I " phone a dentist " for help ?
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Challenge: Plaque ...
« Reply #2 on: April 28, 2005, 05:31:28 PM »
:)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Challenge: Plaque ...
« Reply #3 on: April 28, 2005, 07:08:25 PM »
Code: [Select]
(defun plaque ( x lst / r )
  (setq r (list (car lst)))
  (while (setq lst (cdr lst))
    (setq r (cons x r)
          r (cons (car lst) r))
  )
  (reverse r)
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Challenge: Plaque ...
« Reply #4 on: April 28, 2005, 07:16:55 PM »
Excellent CAB, that would work. :)

Remember ToString?

Given this --

Code: [Select]
(defun Plaque ( x lst )
    (cons (car lst)
        (apply 'append
            (mapcar
               '(lambda (item) (list x item))
                (cdr lst)
            )
        )
    )
)

This is now more elegant to me:

Code: [Select]
(defun ToString ( x / typex )
    (cond
        (   (eq 'str (setq typex (type x)))
            (strcat "\"" x "\"")
        )
        (   (eq 'real typex)
            (rtos x 2 (if (zerop (- x (fix x))) 1 15))
        )
        (   (eq 'list typex)
            (strcat
                (chr 40)
                (if (vl-list-length x)
                    (apply 'strcat
                        (plaque " "
                            (mapcar 'ToString x)
                        )
                    )
                    (strcat
                        (ToString (car x))
                        " . "
                        (ToString (cdr x))
                    )
                )
                (chr 41)
            )
        )
        ((vl-princ-to-string x))
    )
)

Making things like this a real quick snap --

Code: [Select]
(defun ListToCsv ( lst )
    (apply 'strcat
        (plaque ","
            (mapcar 'ToString lst)
        )
    )
)

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