Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/9.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 Data.FixedList源代码中的“Cons”是什么?_Haskell - Fatal编程技术网

Haskell Data.FixedList源代码中的“Cons”是什么?

Haskell Data.FixedList源代码中的“Cons”是什么?,haskell,Haskell,在的源代码中,我找到了以下定义: data FixedList f => Cons f a = (:.) { head :: a, tail :: (f a) } deriving (Eq, Ord) 作为Haskell的新手,很难弄清楚这里发生了什么。我理解诸如datatypename=TypeName{a::Int,b::Int}派生(Show)或datatypename=TypeA | TypeB之类的语法,但上面的代码超出了我的理解范围。如有任何文档/或

在的源代码中,我找到了以下定义:

data FixedList f =>
  Cons f a = (:.) {
    head :: a,
    tail :: (f a)
  }  deriving (Eq, Ord)

作为Haskell的新手,很难弄清楚这里发生了什么。我理解诸如
datatypename=TypeName{a::Int,b::Int}派生(Show)
datatypename=TypeA | TypeB
之类的语法,但上面的代码超出了我的理解范围。如有任何文档/或走查,将不胜感激

这主要是你以前见过的东西的奇特格式

data FixedList f => Cons f a = (:.) { head :: a
                                    , tail :: (f a)
                                    } deriving (Eq, Ord)

fixedlistf
只是
Cons f a
数据类型上的一个类型类约束
(:)
是中缀数据构造函数。

首先,当使用构造函数时,
FixedList f=>
强制执行
f
FixedList
的约束。我不知道这里为什么用它。一般来说是这样

data Cons f a = Cons
  { head :: a
  , tail :: f a
  }
在Haskell中,可以使用以
开头的符号创建中缀数据构造函数。在库中,构造函数放在括号中,因此可以与记录语法一起使用。如果没有记录语法,它将如下所示:

data Cons f a = a :. f a
模式匹配与列表非常相似。下面是一个使用它的简单函数:

mapHead :: FixedList f => (a -> a) -> Cons f a -> Cons f a
mapHead f (a :. as) = f a :. as
这是一个不使用中缀构造函数的定义

data Cons f a = Cons
  { head :: a
  , tail :: f a
  }