接下来的几行应显示其工作方式。

[14,2,344,41,5,666]之后的[(14,2),(2,1),(344,3),(5,1),(666,3)]

["Zoo","School","Net"]之后的[("Zoo",3),("School",6),("Net",3)]

到目前为止,这就是我的代码

zipWithLength :: [a] -> [(a, Int)]
zipWithLength (x:xs) = zipWith (\acc x -> (x, length x):acc) [] xs


我想弄清楚第二行中的问题是什么。

最佳答案

安装number-length软件包后,您可以执行以下操作:

module Test where
import           Data.NumberLength

-- use e.g for list of String
withLength :: [[a]] -> [([a], Int)]
withLength = map (\x -> (x, length x))

-- use e.g for list of Int
withLength' :: NumberLength a => [a] -> [(a, Int)]
withLength' = map (\x -> (x, numberLength x))


例子:

>>> withLength ["Zoo", "bear"]
[("Zoo",3),("bear",4)]
>>> withLength' [14, 344]
[(14,2),(344,3)]

10-08 04:48