blane clorley
blane clorley

Reputation: 99

Haskell Lists within lists

I've a homework question where I have to Define a function which with an input in the form of a list with smaller lists of integers it and sums the numbers in each of the innermost lists and then multiplies the resulting sums with each other.

My code is as follows and obviously doesn't work, any help will be greatly appreciated :)

sumI :: [Int] -> Int
sumI [] = 0
sumI (x:xs) = x + sumI xs

mapQ :: [[Int]] -> Int
mapQ [] = []
mapQ xs = [product (sumI x) | x <- xs]

Upvotes: 2

Views: 1222

Answers (2)

Abizern
Abizern

Reputation: 150615

As it's a homework question - here are a couple of tips.

  1. you can use map to apply a function to each member of a list. As the list is made up of lists, a suitable function may be sum

  2. You want to turn a list into a single number. I mean , you have a list of sums that you need and you want to multiply them together to get a single number. This is quite common, and is handled by one of the many fold functions.

Give these a try.

Upvotes: 5

Jasmijn
Jasmijn

Reputation: 10452

I think you're nearly there. Replace the last line with

mapQ xs = product [ sumI x | x <- xs]

Upvotes: 1

Related Questions