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

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

问题描述

我想在haskell中创建一个函数,给出给定的一位数字列表,我将给出完整的数字.我在考虑使用密集的列表和顾客,如下所示的代码:

I want to make a function in haskell that given a list of single digits, i make the full number. I was thinking in using intensive lists and patrons, as the code it follows:

funcion5 (x:xs) = [y*(10^w) | y <- (x:xs) w]

这个想法是,遍历列表并将每个数字乘以10 pow到数字的位置.最后,我只需要对所有数字求和,就得到这样的数字:

The idea is, to go over the list and multiplie each digit to 10 pow to the position of the number. Finally i only have to sum all digits and I have the number as this:

sum (funcion5 (x:xs))

有人可以帮助我吗?谢谢

Can anyone help me, please? Thanks

推荐答案

可以简单地通过如下折叠foldl1 :: Foldable t => (a -> a -> a) -> t a -> a来完成:

This may simply be done by folding with foldl1 :: Foldable t => (a -> a -> a) -> t a -> a as follows;

Prelude> foldl1 (\x y -> 10*x+y) [1,2,3]
123

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

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