Page 1 of 1

DUP question

Posted: Tue Sep 19, 2006 10:07 pm
by 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?

Posted: Tue Sep 19, 2006 11:48 pm
by Sammo
Will this work?

Code: Select all

(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?

Code: Select all

(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)
()