Home > OS >  Sum of cubes of positive integers of list using recursion (Haskell)
Sum of cubes of positive integers of list using recursion (Haskell)

Time:08-12

I'm trying to make a short Haskell function to compute the sum of the cubes of all positive integers in a list using recursion.

f'' :: [Int] -> Int
f''[] = 0
f'' (x:xs) =  (x^3   f''(xs))   

But I can't figure out how to ensure only positive values of x are accepted.

CodePudding user response:

You can filter, with guards, so then the function will look like:

f'' :: [Int] -> Int
f'' [] = 0
f'' (x:xs)
  | x > 0 = x^3   f''(xs)
  | otherwise = …

where you still need to fill in the part.

  • Related