简体   繁体   中英

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]

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM