ハスケル(そして他の関数型言語のジッパーパターン)について、データ構造を横断して変更する方法について少しお読みになりました。これは良いことだと思いました クラスは、横断されたデータ構造とは無関係に、コードを記述するための共通のトラバーサルインタフェースを提示することができるので、私はHaskellで型クラスを作成する技術を磨くことができます。Haskell:ジッパーのタイプクラスを作成する
私はおそらく二つのクラス必要があるだろうと思った - ルートデータ構造のための1、および第一を横断する を作成した特殊なデータ構造のための1:
module Zipper where
class Zipper z where
go'up :: z -> Maybe z
go'down :: z -> Maybe z
go'left :: z -> Maybe z
go'right :: z -> Maybe z
class Zippable t where
zipper :: (Zipper z) => t -> z
get :: (Zipper z) => z -> t
put :: (Zipper z) => z -> t -> z
をしかし、私はいくつかの簡単なとこれらをしようとしたときリストのようなデータ構造:
-- store a path through a list, with preceding elements stored in reverse
data ListZipper a = ListZipper { preceding :: [a], following :: [a] }
instance Zipper (ListZipper a) where
go'up ListZipper { preceding = [] } = Nothing
go'up ListZipper { preceding = a:ps, following = fs } =
Just $ ListZipper { preceding = ps, following = a:fs }
go'down ListZipper { following = [] } = Nothing
go'down ListZipper { preceding = ps, following = a:fs } =
Just $ ListZipper { preceding = a:ps, following = fs }
go'left _ = Nothing
go'right _ = Nothing
instance Zippable ([a]) where
zipper as = ListZipper { preceding = [], following = as }
get = following
put z as = z { following = as }
またはバイナリツリー:
-- binary tree that only stores values at the leaves
data Tree a = Node { left'child :: Tree a, right'child :: Tree a } | Leaf a
-- store a path down a Tree, with branches not taken stored in reverse
data TreeZipper a = TreeZipper { branches :: [Either (Tree a) (Tree a)], subtree :: Tree a }
instance Zipper (TreeZipper a) where
go'up TreeZipper { branches = [] } = Nothing
go'up TreeZipper { branches = (Left l):bs, subtree = r } =
Just $ TreeZipper { branches = bs, subtree = Node { left'child = l, right'child = r } }
go'up TreeZipper { branches = (Right r):bs, subtree = l } =
Just $ TreeZipper { branches = bs, subtree = Node { left'child = l, right'child = r } }
go'down TreeZipper { subtree = Leaf a } = Nothing
go'down TreeZipper { branches = bs, subtree = Node { left'child = l, right'child = r } } =
Just $ TreeZipper { branches = (Right r):bs, subtree = l }
go'left TreeZipper { branches = [] } = Nothing
go'left TreeZipper { branches = (Right r):bs } = Nothing
go'left TreeZipper { branches = (Left l):bs, subtree = r } =
Just $ TreeZipper { branches = (Right r):bs, subtree = l }
go'right TreeZipper { branches = [] } = Nothing
go'right TreeZipper { branches = (Left l):bs } = Nothing
go'right TreeZipper { branches = (Right r):bs, subtree = l } =
Just $ TreeZipper { branches = (Left l):bs, subtree = r }
instance Zippable (Tree a) where
zipper t = TreeZipper { branches = [], subtree = t }
get TreeZipper { subtree = s } = s
put z s = z { subtree = s }
私はそれをコンパイルすることができなかった、私はちょうど私のZippable
インスタンス定義のそれぞれに対して、このようなエラーの多くを得るだろう:
Zipper.hs:28:14: Couldn't match expected type `z' against inferred type `ListZipper a' `z' is a rigid type variable bound by the type signature for `zipper' at Zipper.hs:10:20 In the expression: ListZipper {preceding = [], following = as} In the definition of `zipper': zipper as = ListZipper {preceding = [], following = as} In the definition for method `zipper'は、だから私はどこここから行くのか分かりません。私は、
(Zipper z) =>
宣言で
z
のいずれかが
Zipper
であることを望んでいるときに、私がこれらの2つのインスタンス を一緒にバインドしようとしていることが問題だと思います。
ジッパーをステート変数として使用してMonadインスタンスを追加する方法について説明します。そして、2つのアイテムを交換するには、 "x < - get; goDown; y < - get; put x; goUp; put y" –
これは私が今夜行ったことです:) http://gist.github.com/115203 – rampion