champagnepaki
champagnepaki

Reputation: 1

How to append a procedure argument to your list - Racket

(define (list-expand L)
  (if (empty? L)
      empty
      (append (helper-method (car L) null)(list-expand (cdr L)))))
(define (helper-method n lst2)
  (if (= n 1) '(1)
      (append lst2 '(n) (helper-method (- n 1) null ))))
Testing list-expand 
Expected: '(4 3 2 1 3 2 1 1), actual: '(n n n 1 n n 1 1)
Expected: '(5 4 3 2 1 2 1), actual: '(n n n n 1 n 1)
Expected: '((7 6 5 4 3 2 1 8 7 6 5 4 3 2 1), actual: '(n n n n n n 1 n n n n n n n 1)

Sorry, I don't know how to enter code in StackOverflow I hope yall don't mind.

Anyways, I know the error is in the helper method when I append '(n) to the list returned by recursion. However, how can I fix this error since I want the value of n to be appended to the list?

Upvotes: 0

Views: 163

Answers (1)

Barmar
Barmar

Reputation: 780673

Don't quote it, call the list function to create a list with variable arguments.

(define (helper-method n lst2)
  (if (= n 1) '(1)
      (append lst2 (list n) (helper-method (- n 1) null ))))

Upvotes: 1

Related Questions