0
votes

I have to write a function that sums the cubes of a list of numbers.

This is my code so far:

cube' :: (Num a) => a -> a 
cube' x = x*x*x


mySum :: (Num a) => [a] -> a 
mySum [] = []
mySum xs = foldr (\acc x -> acc + cube'(x)) 0 xs

The problem is that when I run it I get the following error:

No instance for (Num[t0]) arising from a use of 'it'

In a stmt of an interactive GHCI command: print it

1
You're returning a list in the first clause, did you mean to return 0? Also, foldr will handle an empty list for you so that pattern match isn't useful. you could just do mySum xs = foldr ... without caring what xs is. In this case you can even drop the argument entirely and have the entire definition as mySum = foldr (\acc x -> acc + cube' x) 0bheklilr

1 Answers

1
votes

You're definitely on the right track. As bhelkir points out in a comment, the first clause of the definition is wrong and unnecessary. The other problem is that you have the argument order wrong for the lambda.