I have to sum up the elements of a list like this for example: if your input is ['a','b','c']
, the output should be ["a","ab","abc"]
. But I have no idea how to code this... could somebody give me a hint? I would be very grateful!
all those hints are fine but you probably struggle with those too
I would suggest you start from here:
sumList [] = []
sumList [x] = [[x]]
sumList (x:xs) = ... : map (...) (sumList xs)
and try to figure out what you have to put into both ...
( hint not the same ;))
the first line is only there to give some reasonable result for an empty list as input - you could remove it (recursion should not hit it)
The second one will do [1] -> [[1]]
Now you have to figure out what to do with more - here is an additional hint:
sumList [1,2]
{ 3. line - x = 1, xs = [2] }
= ... : map (...) (sumList [2])
{ 2. line }
= ... : map (...) [[2]]
now you want
= [[1],[1,2]]
so it look like you can do this with
first ... = [1]
second ... = map (prepend 1) to every list in [[2]]
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.