且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Haskell递归列表求和

更新时间:2023-02-27 09:19:48

  listsum [a:b:[]] = a + b 

这里, a:b:[] 表示列表 [a,b] ,所以 [a:b:[]] 表示 [[a,b]] ,这不是您想要的。



只需将其更改为

  listsum(a:b:[])= a + b 

现在,在

  listsum x = head x + listsum tail x 
$ c>

零件 listsum tail x 表示 apply listsum tail ,然后将结果应用于 x 的。我想你的意思是应用 tail x ,然后应用 listsum 到结果,可以表示为 listsum(tail x)






我建议稍微清理一下实现,它假设总结空列表为零。

  listsum [] = 0 
listsum(x:t)= x + listsum t

这与您的实现功能有所不同,因为它可以正确处理带有零个或一个元素的列表。


I'm new to functional programming and haskell, so I'm just starting out learning both by attempting some Euler problems. This involves a great deal of list summations.

So I'm trying to write a recursive list summation function that takes one list as input and returns an integer as output, for instance:

-- Sum up a list
listsum :: [int] -> int
listsum [a:b:[]] = a + b
listsum x = head x + listsum tail x

When compiling this code, I receive this error:

Couldn't match expected type `[[int] -> int]'
            with actual type `[a0] -> [a0]'
Relevant bindings include
  x :: [int]
    (bound at C:\Users\Rade\Documents\GitHub\haskell\euler_2.hs:15:9)
  listsum :: [int] -> int
    (bound at C:\Users\Rade\Documents\GitHub\haskell\euler_2.hs:14:1)
Probable cause: `tail' is applied to too few arguments
In the first argument of `listsum', namely `tail'
In the second argument of `(+)', namely `listsum tail x'

I've tried to research the pattern matching, but I cannot understand what it means with expected type versus actual type. Where am I going wrong?

listsum [a:b:[]] = a + b

Here, a:b:[] means the list [a, b], so [a:b:[]] means [[a, b]], which is not what you want.

Simply change this to

listsum (a:b:[]) = a + b

Now, in

listsum x = head x + listsum tail x

the part listsum tail x means apply listsum to tail, and then apply the result to x. I suppose you meant apply tail to x and then apply listsum to the result, which can be expressed as listsum (tail x).


I suggest a bit cleaner implementation, which assumes that summing empty list yields zero.

listsum [] = 0
listsum (x:t) = x + listsum t

This differs in functionality from your implementation, since it handles lists with zero or one element correctly.