简体   繁体   English

Haskell作业-小数列表[Int]-> Int

[英]Haskell Homework - Decimal List [Int] -> Int

The assignment is to define function decimal :: [Int] -> Int in which a list of positive ints is to give the decimal number 分配是定义函数decimal :: [Int] -> Int ,其中一个正整数列表将给出十进制数 Sigma功能 so that the list [1,4,3,1,9] is to return the Integer 14319. I am to use the fold function. 因此这个列表[1,4,3,1,9]是返回Integer 14319.我使用fold功能。

I don't really have good idea to start here, so I just need a push in the right direction, but I was thinking about the Horner-scheme. 我真的没有从这里开始的好主意,所以我只需要朝着正确的方向推进,但我正在考虑霍纳计划。 Thanks! 谢谢!

In the fold, you start from the left and move towards the right. 在折叠中,您从左侧开始向右移动。 As you consume the next element from the list, you multiply what you already had by 10 and add the new element to that. 消耗列表中的下一个元素时,将您已有的元素乘以10,然后将新元素添加到该元素中。

So if you seed the foldl with 0, and had [1,2,3], your function would multiply current (0) by 10 (also 0), then add 1. Moving on, multiply current (1) by 10 (to get 10) and add 2 (12). 因此,如果将折叠的种子设为0,并且具有[1,2,3],则函数会将电流(0)乘以10(也是0),然后加1。继续,将电流(1)乘以10(得出得到10)并加2(12)。 Then finally for 3, 12 * 10 = 120, 120 + 3 = 123. 然后最后为3,12 * 10 = 120,120 + 3 = 123。

That should be pretty easy to code up :) 那应该很容易编写代码:)

Maybe this equation would guide you. 也许这个方程式将指导您。

S_n = \\ sum_ {k = 0} ^ n x_k 10 ^ {nk} = \\ sum_ {k = 0} ^ {n-1} x_k 10 ^ {nk} + x_n = 10 \\ sum_ {k = 0} ^ { n-1} x_k 10 ^ {((n-1)-k} + x_n = 10S_ {n-1} + x_n

Since this is a homework, let's stop at the suggestion that you expand this expression for some list, and try to extract a recurrent relationship: 由于这是一项家庭作业,因此我们不建议您将此表达式扩展为某些列表,并尝试提取一个经常性关系:

x_0*10^n+x_1*10^(n-1)+...+x_n*10^0 = (((x_0*10+x_1)*10+x_2)...)*10+x_n x_0 * 10 ^ n + x_1 * 10 ^(n-1)+ ... + x_n * 10 ^ 0 =((((x_0 * 10 + x_1)* 10 + x_2)...)* 10 + x_n

If you compare this to folds, you will see one fold matches this pattern for a particular function of two arguments. 如果将它与折叠比较,对于两个参数的特定函数,将看到一个与该模式匹配的折叠。

This is my variant 这是我的变体

import Data.List
decimal :: [Int] -> Int
decimal xs = foldl' (\sum (pos,x) -> (sum + x*(10^(l-pos)))) 0 $ zip [1..] xs where
            l = length xs


*Main> decimal [1,4,3,1,9]
14319

In Haskell , you have really powerfull weapon - functions for lists processing. Haskell ,您拥有真正强大的武器-列表处理功能。 One of these functions is foldl (we use strict version of foldl , foldl' ) It's type 这些功能之一是foldl (我们用严格的版本foldlfoldl' )它的类型

foldl :: (a -> b -> a) -> a -> [b] -> a

This functions takes thre arguments, an accumulating agrument, a list processed, and, the most interest, the function that perform any operation with accumulator and list element and returns the result. 该函数接受三个参数,一个累加的累积量,一个已处理的列表,以及最感兴趣的是对累加器和list元素执行任何操作并返回结果的函数。 Fold is really significant function so you should read detail manual about it. 折叠功能确实很重要,因此您应该阅读有关它的详细手册
But, there is a problem, we have three variables it our equation: list element processed ( x ), total list length ( n ) and position of processed element ( k ). 但是,有一个问题,我们的方程式有三个变量:已处理列表元素( x ),总列表长度( n )和已处理元素的位置( k )。 But we can traverse to foldl only one element. 但是我们只能遍历foldl元素。 How can we traverse position of each element? 我们如何遍历每个元素的位置? Let's form tuples from Int where first element is a position, and second is a value. 让我们从Int组成元组,其中第一个元素是位置,第二个是值。 It is a standard trick, zip function helps us: 这是一个标准技巧, zip功能可帮助我们:

zip [1..] [1,4,3,4,6]
[(1,1),(2,4),(3,3),(4,4),(5,6)]

Than we pass our list of tuples into foldl function, and foldl call lambda function (\\sum (pos,x) -> (sum + x*(10^(l-pos)))) for each element of list, summing result in sum 然后我们将元组列表传递给foldl函数,然后foldl为列表的每个元素调用lambda function (\\sum (pos,x) -> (sum + x*(10^(l-pos)))) ,将结果相加sum

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM