如何在方案中将列表转换为 num?

如何在方案中将列表转换为 num?

问题描述:

比如将 (1 2 3 4) 转换为 1234~

like convert (1 2 3 4) to 1234~

该问题的特点是将列表合并为单个值,强烈建议使用折叠:

The problem is characterized by coalescing a list into a single value, strongly suggesting use of a fold:

(define (fold-left op initial items)
  (define (loop result rest)
    (if (null? rest)
        result
        (loop (op result (car rest))
              (cdr rest))))
  (loop initial items))

(define (list->num list)
  (fold-left (lambda (value digit)
                     (+ (* value 10) digit))
             0
             list))

(list->num '(1 2 3 4))
;Value: 1234