Converting a list of digits to a number

冷暖自知 提交于 2020-01-05 12:17:09

问题


I was wondering if there was a way to take a list of numbers (digits), and truncate the numbers together to be one large number (not addition) in Scheme. For example, I would want

(foo '(1 2 3 4))
;=> 1234

Does Scheme have a built in function to do this?


回答1:


There are a number of languages that are in the Scheme family, and there are a few versions of Scheme, too. If you're using one, e.g., Racket, that includes a left associative fold (often called foldl, fold, or reduce, though there are other variations, too), this is pretty straightfoward to implement in terms of the fold. Folds have been described in more detail in these questions and answers:

  • Finding maximum distance between two points in a list (scheme) This question includes a description of how fold can be viewed as an iterative construct (and in Scheme, which mandates tail call optimization, is compiled to iterative code), and also includes an implementation of foldl for Schemes that don't have it.
  • Flattening a List of Lists This question is about a somewhat unusual fold, and how it (or a standard fold) can be used to flatten a list.
  • scheme structures and lists This question has an example of how you might adjust the function that you pass to a fold to achieve slightly different behavior. (I also include an opinionated (but true ;), I assure you) comment about how Common Lisp's reduce provides a somewhat more convenient interface than what's provided in some of the Scheme libraries.

Here's what the code looks like in terms of foldl:

(define (list->num digits)
  (foldl (lambda (digit n)
           (+ (* 10 n) digit))
         0
         digits))
> (list->num '(1 2 3 4))
1234 

If your language doesn't have it, foldl is pretty easy to write (e.g., my answer to the one of the questions above includes an implementation) and use the preceding code, or you can write the whole function (using the same approach) yourself:

(define (list->num-helper digits number-so-far)
  (if (null? digits)
      number-so-far
      (list->num-helper (cdr digits)
                        (+ (* 10 number-so-far)
                           (car digits)))))

(define (list->num digits)
  (list->num-helper digits 0))

You can make that a bit more concise by using a named let:

(define (list->num digits)
  (let l->n ((digits digits)
             (number 0))
    (if (null? digits)
        number
        (l->n (cdr digits)
              (+ (* 10 number)
                 (car digits))))))


来源:https://stackoverflow.com/questions/19554279/converting-a-list-of-digits-to-a-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!