DUP question

Started by Jeremy Dunn, September 19, 2006, 03:07:38 PM

Previous topic - Next topic

Jeremy Dunn

With the DUP function if I write



(dup '(2 3) 3) this is ((2 3)(2 3)(2 3))



but what if I want (2 3 2 3 2 3) ?



I could write (flat (dup '(2 3) 3)) but that is a little cumbersome. Could the DUP function be modified so that I could write (dup 2 3 3) where it is understood that multiple previous arguments are duplicated in order?

Sammo

#1
Will this work?
(define (DUP)
  (flat (dup (chop (args)) ((args) -1))))

> (DUP 1 2 3)

(1 2 1 2 1 2)

> (DUP 1 10)

(1 1 1 1 1 1 1 1 1 1)

> (DUP '(1 2) 10)

(1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2)

> (DUP '(1 2) "A" 5)

(1 2 "A" 1 2 "A" 1 2 "A" 1 2 "A" 1 2 "A")

> (DUP 5)

()



Or this?
(define (DUP)
  (apply append (dup (chop (args)) ((args) -1))))

> (DUP 1 2 3)

(1 2 1 2 1 2)

> (DUP 1 10)

(1 1 1 1 1 1 1 1 1 1)

> (DUP '(1 2) 10)

((1 2) (1 2) (1 2) (1 2) (1 2) (1 2) (1 2) (1 2) (1 2) (1 2))

> (DUP '(1 2) "A" 5)

((1 2) "A" (1 2) "A" (1 2) "A" (1 2) "A" (1 2) "A")

>  (DUP 5)

()