Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
可在Haskell中遍历_Haskell_Algebraic Data Types - Fatal编程技术网

可在Haskell中遍历

可在Haskell中遍历,haskell,algebraic-data-types,Haskell,Algebraic Data Types,我试图从Data.Traversable文档中理解这个示例 data Tree a = Empty | Leaf a | Node (Tree a) a (Tree a) instance Traversable Tree where traverse f Empty = pure Empty traverse f (Leaf x) = Leaf <$> f x -- confusion 数据树a=空|叶a |节点(树a)a(树a) 实例可

我试图从
Data.Traversable
文档中理解这个示例

 data Tree a = Empty | Leaf a | Node (Tree a) a (Tree a)

instance Traversable Tree where
    traverse f Empty        = pure Empty
    traverse f (Leaf x)     = Leaf <$> f x -- confusion
数据树a=空|叶a |节点(树a)a(树a)
实例可遍历树,其中
遍历f Empty=纯空
遍历f(叶x)=叶f x--混淆

如何应用
叶f x
Leaf
不是函数,仍然可以使用它。

Leaf
是构造函数,因此是函数。在这种情况下,它具有类型
a->Tree a
。请参阅。

叶是一个函数

如果使用GADT语法,这一点会立即变得明显:

data Tree a where
    Empty :: Tree a
    Leaf  :: a -> Tree a
    Node  :: Tree a -> a -> Tree a -> Tree a

它有助于将
树写入:

这表明
Leaf::a->treea
是一个函数。我们可以明确说明
树的类型

import Data.Kind

data Tree :: Type -> Type where
  ..
import Data.Kind

data Tree :: Type -> Type where
  ..