DUP question

For the Compleat Fan
Locked
Jeremy Dunn
Posts: 95
Joined: Wed Oct 13, 2004 8:02 pm
Location: Bellingham WA

DUP question

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

Sammo
Posts: 180
Joined: Sat Dec 06, 2003 6:11 pm
Location: Loveland, Colorado USA

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

Locked