将数字列表转换为数字 [英] Converting a list of digits to a number

查看:131
本文介绍了将数字列表转换为数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种方法可以获取数字列表(数字),然后在Scheme中将这些数字一起截断为一个大数字(而不是加法).例如,我想要

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

Scheme是否具有内置功能来做到这一点?

Does Scheme have a built in function to do this?

推荐答案

Scheme系列中有多种语言,Scheme也有一些版本.如果您使用的是例如球拍这样的球拍,其中包括左联想折(通常也称为foldlfoldreduce,尽管也有其他变化),这在实现方面非常简单褶皱.这些问题和答案中已对折痕进行了更详细的描述:

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:

  • 查找列表中两个点之间的最大距离(方案)该问题包括对fold的描述被视为迭代构造(并且在Scheme中要求进行尾部调用优化,并被编译为迭代代码),并且还包括foldl的实现,用于没有它的Schemes.
  • 对列表列表进行平整这个问题是关于不寻常的折痕以及它的折皱方式(或标准折痕)可用于拉平列表.
  • 方案结构和列表这个问题有一个示例,说明如何调整传递给折叠的功能实现略有不同的行为. (我也向您保证,但是我的看法是真实的;),向您保证,关于Common Lisp的reduce如何提供比某些Scheme库所提供的界面更方便的界面.
  • 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.

这是根据foldl的代码形式:

(define (list->num digits)
  (foldl (lambda (digit n)
           (+ (* 10 n) digit))
         0
         digits))

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

如果您的语言没有foldl很容易编写(例如,我对上述问题之一的答案包括一个实现)并使用前面的代码,或者您可以自己编写整个函数(使用相同的方法):

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))

通过使用命名为let

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

这篇关于将数字列表转换为数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆