【发布时间】:2019-05-09 14:00:36
【问题描述】:
我刚刚第二次阅读Apfelmus' excellent introduction to Finger Trees,开始怀疑他对head的实现:
import Prelude hiding (head)
data Tree v a = Leaf v a
| Branch v (Tree v a) (Tree v a)
toList :: Tree v a -> [a]
toList (Leaf _ a) = [a]
toList (Branch _ x y) = toList x ++ toList y
head :: Tree v a -> a
head (Leaf _ a) = a
head (Branch _ x _) = head x
由于相互实现功能是一种非常好的重用代码的方式,这让我开始思考下面的实现是否会像他的原始实现一样高效(复杂性):
import Prelude -- not needed, just for making it obvious
data Tree v a = Leaf v a
| Branch v (Tree v a) (Tree v a) deriving Show
toList :: Tree v a -> [a]
toList (Leaf _ a) = [a]
toList (Branch _ x y) = toList x ++ toList y
head' :: Tree v a -> a
head' = head . toList
惰性求值是否与原始实现一样高效?
【问题讨论】:
标签: haskell time-complexity finger-tree