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
2010-03-14 21:08:57
Couldn't it just be `(cons x ...)` instead of `(append (list x) ...)` ?
Nathan Sanders
2010-03-14 21:15:37
@ Nathan - Correct and fixed
Mimisbrunnr
2010-03-14 21:17:48
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
2010-03-23 19:56:33