【发布时间】:2015-11-02 01:02:21
【问题描述】:
我有一个类似 Set 的数据结构,实现为 Trie,其定义如下:
import qualified Data.Map as M
import Data.Foldable (Foldable, foldr)
import Prelude hiding (foldr)
import Data.Maybe (fromMaybe)
data Trie a = Trie { endHere :: Bool
, getTrie :: M.Map a (Trie a)
} deriving (Eq)
还有一个如下所示的插入操作:
insert :: (Ord a, Foldable f) => f a -> Trie a -> Trie a
insert = foldr f (\(Trie _ m) -> Trie True m) where
f e a = overMap (M.alter (Just . a . fromMaybe (Trie False M.empty)) e)
overMap :: Ord b => (M.Map a (Trie a) -> M.Map b (Trie b)) -> Trie a -> Trie b
overMap f (Trie e m) = Trie e (f m)
我可以得到foldr 的种类,看起来像这样:
foldrTrie :: ([a] -> b -> b) -> b -> Trie a -> b
foldrTrie f i (Trie a m) = M.foldrWithKey ff s m where
s = if a then f [] i else i
ff k = flip (foldrTrie $ f . (k :))
但我不知道Trie 的Foldable 实例。 foldrTrie 似乎具有所有必要的功能,但我就是不知道类型。
这是我正在寻找的 foldr 行为示例:
fromList :: (Ord a, Foldable f, Foldable g) => f (g a) -> Trie a
fromList = foldr insert (Trie False M.empty)
toList :: (Ord a) => Trie a -> [[a]]
toList = foldr (:) [] -- replace foldr here with foldrTrie and you'll get the
-- desired behaviour
toList (fromList ["abc", "def"]) -- ["abc","def"]
我无法管理的是Foldable 的类型签名:
instance Foldable Trie a where
我尝试让我的Trie 有第二个类型参数:
data Trie a (f a) = Trie { endHere :: Bool
, getTrie :: M.Map a (Trie a (f a))
} deriving (Eq)
这样我就可以做这样的事情:
instance Foldable Trie a f where
foldr f i (Trie a m) = M.foldrWithKey ff s m where
s = if a then f [] i else i
ff k = flip (foldrTrie $ f . (k :))
但我无法弄清楚类型。
一个更通用的问题框架可能是这样的:如果我有一个可以存储 only 列表的数据结构,我是否能够在该数据结构上定义foldr,所以它将它存储的列表视为每个元素?该数据结构的类型是什么样的?
【问题讨论】:
-
instance Foldable Trie where foldr f = foldrTrie $ flip $ foldr f。我看不出为什么“Trie 只能存储可折叠的东西”。 -
此 Trie 旨在作为一种数据结构来存储
Ord元素的序列(或更一般地说,Foldables)。存储在Trie中的东西必须 是Foldable,因为这是向Trie插入东西的唯一方法。我希望能够使用foldr再次访问Trie中的内容,并保留它们类似序列的结构。 -
我确信有很多方法可以表示一个序列,但我看不到
Foldable是如何做到的。仅使用Trie的Foldable接口,我认为没有办法编写您想要的函数,因为Foldable根本不会这样做。foldrTrie比foldr : (a -> b -> b) -> b -> Trie a -> b更通用。 -
我想我要问的是,如果我有一个可以存储only(比如)列表的数据结构,我可以在该数据结构上实现
Foldable吗?我可以管理机制(我认为);但这是阻碍我的类型。该结构的类型签名是什么样的?