Haskell - 使用递归的代数数据类型?
Haskell - Algebraic data types that use recursion?
我按照指南创建了使用以下数据类型的二叉搜索树:
data BinarySearchTree a = EmptyTree | TreeNode a (BinarySearchTree a) (BinarySearchTree a) deriving (Show, Read, Eq)
我说 'TreeNode' 使用递归是否正确,即创建 2 个其自身数据类型的元素 '(BinarySearchTree a) (BinarySearchTree a)'?
我从未见过这样的数据类型,任何简短的解释都会很棒!
是的,这是一个递归数据类型。
我推荐 Learn You A Haskell For Great Good 中的相关章节 - 非常适合初学者。它也描述了您的具体情况:
Here's what we're going to say: a tree is either an empty tree or it's
an element that contains some value and two trees. Sounds like a
perfect fit for an algebraic data type!
我按照指南创建了使用以下数据类型的二叉搜索树:
data BinarySearchTree a = EmptyTree | TreeNode a (BinarySearchTree a) (BinarySearchTree a) deriving (Show, Read, Eq)
我说 'TreeNode' 使用递归是否正确,即创建 2 个其自身数据类型的元素 '(BinarySearchTree a) (BinarySearchTree a)'?
我从未见过这样的数据类型,任何简短的解释都会很棒!
是的,这是一个递归数据类型。
我推荐 Learn You A Haskell For Great Good 中的相关章节 - 非常适合初学者。它也描述了您的具体情况:
Here's what we're going to say: a tree is either an empty tree or it's an element that contains some value and two trees. Sounds like a perfect fit for an algebraic data type!