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
0
? Also,foldr
will handle an empty list for you so that pattern match isn't useful. you could just domySum xs = foldr ...
without caring whatxs
is. In this case you can even drop the argument entirely and have the entire definition asmySum = foldr (\acc x -> acc + cube' x) 0
– bheklilr