Convert number to list of digits

前端 未结 3 1077
滥情空心
滥情空心 2020-12-21 06:29

How do I convert a number to a list of digits?

I am currently doing:

;; (num->list 12345) -> \'(1 2 3 4 5)
(define (num->list n)
  (local 
          


        
3条回答
  •  孤城傲影
    2020-12-21 07:08

    Here's how I'd do it in Racket:

    (require srfi/1 srfi/26)
    (define (digits->list num (base 10))
      (unfold-right zero? (cut remainder <> base) (cut quotient <> base) num))
    

    This is the sort of problem unfold was designed for. :-D

提交回复
热议问题