views:

123

answers:

2

I want to define a method that take an integer as input and creates dynamically a list of all descending integer numbers to zero. I find trouble into calling method for the n-1 element

+4  A: 

It's not all that pretty but this should work, tested in DrScheme.

(define (gen-list x )
  (if (= x 0) (list 0) (cons x (gen-list (- x 1)))))
Mimisbrunnr
Couldn't it just be `(cons x ...)` instead of `(append (list x) ...)` ?
Nathan Sanders
@ Nathan - Correct and fixed
Mimisbrunnr
A: 

If you're using PLT scheme, the comprehensions library will let you do this rather neatly:

; natural -> (listof natural)
(define (list-to-zero start-num)
 (for/list ([i (in-range start-num 0 -1)])
  i))

Just an alternative to the recursive form...

David Brooks