Reputation: 145
I know that
(define (repe k n) (make-list k n))
compose a list where n appears k times but... How can I construct a similar sentence for which k is the first element and n the second of a previous list?
My
(define (repe x) (make-list car(x) cdr(x)) list)
does not seem to work.
On the other hand, I want the second element of the list n not to be a number but a letter. How can it be done (since make-list seems to be defined just for numbers)?
Upvotes: 0
Views: 373
Reputation: 66371
Functions are applied like this: (car x)
and (cdr x)
, not like car(x)
and cdr(x)
.
And (cdr x)
is a list - the second element is (car (cdr x))
, or (cadr x)
for short.
Your description isn't entirely clear, but it seems like you're looking for
(define (repe xs) (make-list (car xs) (cadr xs)))
Examples:
> (repe (list 4 #\Z))
'(#\Z #\Z #\Z #\Z)
> (repe (list 3 "hello"))
'("hello" "hello" "hello")
> (repe '(2 (+ 1 1)))
'((+ 1 1) (+ 1 1))
Upvotes: 1