在列表中分散分隔符有两种可能性:
[x1, sep, x2, sep, .. xn][sep, x1, sep, x2, .. sep, xn]使用Data.List中的"intersperse“函数:
λ> intersperse 0 [1..5]
[1,0,2,0,3,0,4,0,5]
λ> 0 : intersperse 0 [1..5]
[0,1,0,2,0,3,0,4,0,5]但是,可以使用隐藏(非导出)函数“prependToAll”简化第二种情况:
λ> prependToAll 0 [1..5]
[0,1,0,2,0,3,0,4,0,5]为什么,在prependToAll库中,间接字是导出的,而双重却不是?
发布于 2015-05-12 16:02:30
来自 source code
...
-- Not exported:
-- We want to make every element in the 'intersperse'd list available
-- as soon as possible to avoid space leaks. Experiments suggested that
-- a separate top-level helper is more efficient than a local worker.
...发布于 2015-05-12 18:58:14
prependToAll并不是一个清晰的名字。我早就猜到了
prependToAll x = map (x :)所以prependToAll :: a -> [[a]] -> [[a]]。
只将您想要内联的函数写成concatMap或foldr比为它想出一个好名字要容易得多。
https://stackoverflow.com/questions/30195775
复制相似问题