0

Here is a list of numbers that when takeD is called it will generate a list base off the previous list. For example if you say (takeD 3 L1) you will get a list (1 2 3). intlistNew creates a list from a different list starting from the number given. For example (takeD 3 (delay (intlistNew 3 100))) will return (3 4 5). I am trying to figure out a way to change intListNew that I don't need to type in 100 as an argument just 3. I can use your help. thanks.

(define L1 (list 1 2 3 4 5 6 7 8 9))

(define (takeD n L)
(if (= n 0) '()
  (cons (car (force L)) (takeD (- n 1) (cdr (force L))))))

(define (intlistNew m n)
(if (> m n) '() (cons m (delay (intlistNew (+ 1 m) n)))))

(takeD 3 (delay (intlistNew 3 100)))
Jonathan
  • 197
  • 3
  • 8

1 Answers1

1

Assuming that “I don't need to type in 100 as an argument just 3” means “I want that intListNew can generate a list with an indefinite number of elements”, then you simply remove the parameter from the function, as well as the recursion termination test:

(define (intlistNew m)
  (cons m (delay (intlistNew (+ 1 m)))))

(takeD 3 (delay (intlistNew 3)))

You can do this because of the delayed execution, which avoids the infinite execution of the function due to the recursive call.

Renzo
  • 26,848
  • 5
  • 49
  • 61