我想将函数的结果显示为列表而不是数字。
我的结果是:
(define lst (list ))
(define (num->base n b)
(if (zero? n)
(append lst (list 0))
(append lst (list (+ (* 10 (num->base (quotient n b) b)) (modulo n b))))))
出现下一个错误:
expected: number?
given: '(0)
argument position: 2nd
other arguments...:
10
我认为你必须重新思考这个问题。将结果附加到全局变量绝对不是可行的方法,让我们通过尾递归尝试不同的方法:
(define (num->base n b)
(let loop ((n n) (acc '()))
(if (< n b)
(cons n acc)
(loop (quotient n b)
(cons (modulo n b) acc)))))
它按预期工作:
(num->base 12345 10)
=> '(1 2 3 4 5)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)