我有两个问题,使用map和length第一个应该给我回字数,但它只计算列表中的元素.
countWords :: [String]-> Int
countWords xs = length (map (words) xs)
countWords ["asd qwe", "-- Foo", "", "\thello world "] => 4--instead it have six words
Run Code Online (Sandbox Code Playgroud)
第二个是棘手的,因为它应该为整个列表返回一个int.我只计算各个元素的字符,而不是整数.
countChars :: [String]-> [Int] --it should be Int
countChars xs = map (\w -> length (w)) xs
countChars ["asd qwe", "-- Foo", "", "\thello world "] => [8,6,0,13]--it should give back the sum of this list which is 27
Run Code Online (Sandbox Code Playgroud)