这里有一个免费的点对点版
import Control.Category (>>>)
import Data.Maybe (catMaybes)
skips =
pred >>> -- \n -> n-1
(`replicate` const Nothing) >>> -- \n -> [ const Nothing, ..., const Nothing ] -- length n -1
(Just:) >>> -- \n -> [ Just, const Nothing, ... const Nothing ] -- length n
cycle >>> -- \n -> [ Just, const Nothing, ... const Nothing, Just, const Nothing, ... ] -- infinite length
zipWith ($) >>> -- \n [ a0, a1, .., aN, aNPlus1, ... ] -> [ Just a0, Nothing, ..., Just aN, Nothing, ... ]
(catMaybes .) -- \n [ a0, a1, .., aN, aNPlus1, ... ] -> [a0, aN, a2N, ...]
正如其他人所说,这样的事情将是更容易理解 与贴题的定义。
我使用>>>
(又名flip (.)
)的唯一原因是您可以更轻松地按照文档进行操作。一个等价的定义是:
skips = (catMaybes .) . zipWith ($) . cycle . (Just:) . (`replicate` const Nothing) . pred
两个自由点名堂这是很好的凸显:
(`replicate` const Nothing)
相当于(flip replicate (const Nothing))
或(\n -> replicate n (const Nothing))
(catMaybes .) . f
相当于\n -> catMaybes . f n
或\n xs -> catMaybes (f n xs)
如果您不能导入catMaybes
,您可以通过其自由点清晰度concatMap (maybe [] return)
E放置它,使无进口定义:
skips = (concatMap (maybe [] return) .) . zipWith ($) . cycle . (Just:) . (`replicate` const Nothing) . pred
可以简化为
skips = (concat.) . zipWith ($) . cycle . (return:) . (`replicate` const []) . pred
你为什么需要? Pointfree功能很有趣,但很少能成为解决您的问题的更好解决方案。此外,与Haskell可以做的相比,这个实现相当复杂。 – bheklilr
只是大学的任务来实现测试的功能(我实现它的参数,但应该是PointFree – Spamua3
@ Spamu3这个实现将是非常困难的免费,我会建议使用更多的功能从'前奏曲'首先工作。这将更容易使点免费 – bheklilr