如何将数字转换为数字列表?
我目前在做:
;; (num->list 12345) -> '(1 2 3 4 5)
(define (num->list n)
(local
((define (num->list n)
(map (lambda (c)
(char->num c))
(string->list (number->string n))))
(define (char->num c)
(- (char->integer c) 48)))
(num->list n)))
Run Code Online (Sandbox Code Playgroud)
但想知道是否有更好的方法.
以下是我在Racket中的表现:
(require srfi/1 srfi/26)
(define (digits->list num (base 10))
(unfold-right zero? (cut remainder <> base) (cut quotient <> base) num))
Run Code Online (Sandbox Code Playgroud)
这是unfold为此而设计的那种问题.:-D