For Programmers: Free Programming Magazines  


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
kiat

2004-07-13, 11:17 pm

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 sing 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 :)
kiat

2004-07-14, 5:14 am

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 '())
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com