<分区>
我被要求为以下二叉树实现一个函数:
data BinaryTree a = Nil | BNode a (BinaryTree a) (BinaryTree a)
我需要实现的函数应该生成一个完整的、对称的、无限的、a
的二叉树,并且应该有签名:
infTree :: a -> BinaryTree a
我该如何实现?
<分区>
我被要求为以下二叉树实现一个函数:
data BinaryTree a = Nil | BNode a (BinaryTree a) (BinaryTree a)
我需要实现的函数应该生成一个完整的、对称的、无限的、a
的二叉树,并且应该有签名:
infTree :: a -> BinaryTree a
我该如何实现?
最佳答案
您可以创建一个循环引用,其中两个子节点都是父节点。
infTree :: a -> BinaryTree a
infTree x = tree
where
tree = BNode x tree tree
这与 repeat
的方式相同功能实现:
repeat :: a -> [a]
repeat x = xs where xs = x : xs
关于haskell - 如何产生无限二叉树?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56063689/