小编Ako*_*szi的帖子

在Haskell中组合地图和长度

我有两个问题,使用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)

haskell list map-function

-1
推荐指数
1
解决办法
212
查看次数

标签 统计

haskell ×1

list ×1

map-function ×1