是否有一个标准的 Haskell 函数,比如 iterate 什么都不停止?
Is there a standard Haskell function like iterate that stops for Nothing?
我想知道是否有一个标准函数迭代一个函数 returns a (Maybe value) over a initial value, collect the values in a list, but ending the list when it gets Nothing .这个函数可以这样实现,例如:
iterateMaybe f a = a : iterMaybe (f a) where
iterMaybe Nothing = []
iterMaybe (Just a) = a : iterMaybe (f a)
或像这样略有不同:
iterateMaybe' f Nothing = []
iterateMaybe' f (Just a) = a : iterateMaybe' f (f a)
None 个 the functions that Hoogle finds 匹配。
这是unfoldr
的特例。
iterateMaybe f = unfoldr (fmap (\s -> (s,s)) . f)
不同之处在于 unfoldr
返回的列表不包含初始元素。
我想知道是否有一个标准函数迭代一个函数 returns a (Maybe value) over a initial value, collect the values in a list, but ending the list when it gets Nothing .这个函数可以这样实现,例如:
iterateMaybe f a = a : iterMaybe (f a) where
iterMaybe Nothing = []
iterMaybe (Just a) = a : iterMaybe (f a)
或像这样略有不同:
iterateMaybe' f Nothing = []
iterateMaybe' f (Just a) = a : iterateMaybe' f (f a)
None 个 the functions that Hoogle finds 匹配。
这是unfoldr
的特例。
iterateMaybe f = unfoldr (fmap (\s -> (s,s)) . f)
不同之处在于 unfoldr
返回的列表不包含初始元素。