我遇到了以下代码:
data Nested a = a :<: (Nested [a]) | Epsilon
mylength Epsilon = 0
mylength (_ :<: xs) = 1 + mylength xs
a :<: (Nested [a])
是什么意思?我不理解这些代码(特别是:<:
)
答案 0 :(得分:4)
:<:
是一个数据构造函数,如Just
。唯一的区别是它是中缀,因为它是象征性的。中缀数据构造函数必须以:
开头。
您可以将上述内容重写为:
data Nested a = Nest a (Nested [a]) | Epsilon
mylength Epsilon = 0
mylength (Nest _ xs) = 1 + mylength xs