Home > Archive > Scheme > July 2004 > getting rid of extra parentheses
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
getting rid of extra parentheses
|
|
|
| Hi,
Could someone help me with the task below? If you're concerned that I might be a student doing Scheme as a curriculum subject and I'm s ing quick solutions here, rest assured that I'm not. I'm self-learning Scheme to improve my programming skills.
;;;;
Exercise 2.8.3
Define the procedure make-list, which takes a nonnegative integer n and an object and returns a new list, n long, each element of which is the object.
(make-list 7 '())
=> (() () () () () () ())
;;;;
This is as far as I've got:
code:
(define make-list
(lambda (n obj)
(cond
((= n 1)
obj)
(list (list obj (make-list (- n 1) obj))))))
(make-list 3 '())
=> (() (() ()))
Thanks in advance :) | |
|
| I think I may have found the solution:
code:
(define make-list
(lambda (n obj)
(cond
((= n 1)
(list obj))
(list (cons obj (append (make-list (- n 1) obj)))))))
(make-list 7 '())
|
|
|
|
|