【问题标题】:How to append a procedure argument to your list - Racket如何将过程参数附加到您的列表 - 球拍
【发布时间】:2020-03-18 00:12:42
【问题描述】:
(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)

抱歉,我不知道如何在 StackOverflow 中输入代码,希望大家不要介意。

无论如何,当我将'(n) 附加到递归返回的列表时,我知道错误出在辅助方法中。但是,由于我希望将 n 的值附加到列表中,如何解决此错误?

【问题讨论】:

标签: scheme racket


【解决方案1】:

不要引用它,调用list函数来创建一个带有可变参数的列表。

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

【讨论】:

  • 你是个传奇。非常感谢
猜你喜欢
  • 2011-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-10
  • 1970-01-01
  • 2015-07-09
  • 2016-06-17
相关资源
最近更新 更多