Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/10.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 - Fatal编程技术网

Haskell 函数,该函数接受二叉树并按顺序输出其值

Haskell 函数,该函数接受二叉树并按顺序输出其值,haskell,Haskell,我目前正在编写一个函数,以获取一个二叉树,并按正确的顺序以横向顺序打印其值。我遇到的问题是,每次调用该函数时,我都会遇到一个非穷举模式错误 --k is the key and d is the value -- l is left tree and r is right tree treeprint (Node l k d r) = treeprint l ++ show k ++ show d ++ treeprint r treeprint Nil="" 你似乎已经解

我目前正在编写一个函数,以获取一个二叉树,并按正确的顺序以横向顺序打印其值。我遇到的问题是,每次调用该函数时,我都会遇到一个非穷举模式错误

 --k is the key and d is the value
 -- l is left tree and r is right tree
treeprint (Node l k d r)      =  treeprint l  ++ show k ++ show d ++ treeprint r 
treeprint Nil=""
你似乎已经解决了你的问题,只是你需要一些括号和空格

您当前的版本 我猜你在用

data Tree k d = Nil | Node (Tree k d) k d (Tree k d)
虽然你没说。我将定义一两个示例树:

example1, example2 :: Tree Int String
example1 = Node (Node Nil 4 "Hello" Nil) 7 "there" (Node Nil 21 "hi" Nil)
example2 = Node example1 34 "well" (Node Nil 55 "This" (Node (Node Nil 73 "one's" Nil) 102 "much" (Node Nil 132 "bigger" Nil)))
你的职能

treeprint (Node l k d r) =  treeprint l  ++ show k ++ show d ++ treeprint r 
treeprint Nil = ""
编译正常,但由于没有空格或括号,输出令人困惑:

*Main> putStrLn $ treeprint example1
4"Hello"7"there"21"hi"
*Main> putStrLn $ treeprint example2
4"Hello"7"there"21"hi"34"well"55"This"73"one's"102"much"132"bigger"
它是有序的,但被挤压在一起,树结构消失了

更多的括号,更多的空格,更清晰 让我们在每棵树周围用括号和空格重写它:

tree_print (Node l k d r) = " (" ++ treeprint l  ++ show k ++ ":" ++ show d ++ treeprint r ++ ") "
tree_print Nil = ""
所以现在更清楚了:

*Main> putStrLn $ tree_print example1
 ( (4:"Hello") 7:"there" (21:"hi") ) 
*Main> putStrLn $ tree_print example2
 ( ( (4:"Hello") 7:"there" (21:"hi") ) 34:"well" (55:"This" ( (73:"one's") 102:"much" (132:"bigger") ) ) ) 
把树压平,得到更多的支架! 也许你不想要括号,因为按顺序打印是为了使树变平。你可以保留空格和空格,这样就行了。或者,定义

toList :: Tree k d -> [(k,d)]
toList Nil = []
toList (Node t1 k d t2) = toList t1 ++ (k,d):toList t2
这意味着您可以将Show实例用于列表:

*Main> toList example1
[(4,"Hello"),(7,"there"),(21,"hi")]
*Main> toList example2
[(4,"Hello"),(7,"there"),(21,"hi"),(34,"well"),(55,"This"),(73,"one's"),(102,"much"),(132,"bigger")]
漂亮的 下面是一种使用库中的Data.tree.Pretty打印树的方法

我必须做一些导入工作:

import qualified Data.Tree as T
import Data.Tree.Pretty
我已经导入了Data.Tree,因为它还定义了一个数据构造函数节点。这意味着当我指的是导入的树时,我将使用T.Node,但当我指的是您的树时,只使用Node

它使用玫瑰树,每个节点可以有任意数量的子树:

data Tree a = Node {
        rootLabel :: a,         -- ^ label value
        subForest :: Forest a   -- ^ zero or more child trees
    }
并定义drawVerticalTree::Tree String->String,我将使用它。 我们需要做的就是将您的树转换为此树,然后我们就可以开展业务了:

toTree :: (Show k,Show d) => Tree k d -> T.Tree String
toTree Nil = T.Node "-" []
toTree (Node t1 k d t2) = T.Node (show k ++ ":" ++ show d) [toTree t1,toTree t2]
现在,让我们在IO monad中显示生成字符串并打印输出:

showtree :: (Show k, Show d) => Tree k d -> String
showtree = drawVerticalTree.toTree

printtree :: (Show k, Show d) => Tree k d -> IO ()
printtree = putStrLn.('\n':).showtree
这提供了很好的输出:

*Main> printtree example1

    7:"there"     
        |         
     ---------    
    /         \   
4:"Hello"  21:"hi"
    |         |   
    --        --  
   /  \      /  \ 
   -  -      -  - 
并相当优雅地处理了稍大的示例:

*Main> printtree example2

                   34:"well"                   
                       |                       
         ------------------------              
        /                        \             
    7:"there"                55:"This"         
        |                        |             
     ---------       -------------             
    /         \     /             \            
4:"Hello"  21:"hi"  -         102:"much"       
    |         |                   |            
    --        --            ------------       
   /  \      /  \          /            \      
   -  -      -  -      73:"one's"  132:"bigger"
                           |            |      
                           --           --     
                          /  \         /  \    
                          -  -         -  -    
你似乎已经解决了你的问题,只是你需要一些括号和空格

您当前的版本 我猜你在用

data Tree k d = Nil | Node (Tree k d) k d (Tree k d)
虽然你没说。我将定义一两个示例树:

example1, example2 :: Tree Int String
example1 = Node (Node Nil 4 "Hello" Nil) 7 "there" (Node Nil 21 "hi" Nil)
example2 = Node example1 34 "well" (Node Nil 55 "This" (Node (Node Nil 73 "one's" Nil) 102 "much" (Node Nil 132 "bigger" Nil)))
你的职能

treeprint (Node l k d r) =  treeprint l  ++ show k ++ show d ++ treeprint r 
treeprint Nil = ""
编译正常,但由于没有空格或括号,输出令人困惑:

*Main> putStrLn $ treeprint example1
4"Hello"7"there"21"hi"
*Main> putStrLn $ treeprint example2
4"Hello"7"there"21"hi"34"well"55"This"73"one's"102"much"132"bigger"
它是有序的,但被挤压在一起,树结构消失了

更多的括号,更多的空格,更清晰 让我们在每棵树周围用括号和空格重写它:

tree_print (Node l k d r) = " (" ++ treeprint l  ++ show k ++ ":" ++ show d ++ treeprint r ++ ") "
tree_print Nil = ""
所以现在更清楚了:

*Main> putStrLn $ tree_print example1
 ( (4:"Hello") 7:"there" (21:"hi") ) 
*Main> putStrLn $ tree_print example2
 ( ( (4:"Hello") 7:"there" (21:"hi") ) 34:"well" (55:"This" ( (73:"one's") 102:"much" (132:"bigger") ) ) ) 
把树压平,得到更多的支架! 也许你不想要括号,因为按顺序打印是为了使树变平。你可以保留空格和空格,这样就行了。或者,定义

toList :: Tree k d -> [(k,d)]
toList Nil = []
toList (Node t1 k d t2) = toList t1 ++ (k,d):toList t2
这意味着您可以将Show实例用于列表:

*Main> toList example1
[(4,"Hello"),(7,"there"),(21,"hi")]
*Main> toList example2
[(4,"Hello"),(7,"there"),(21,"hi"),(34,"well"),(55,"This"),(73,"one's"),(102,"much"),(132,"bigger")]
漂亮的 下面是一种使用库中的Data.tree.Pretty打印树的方法

我必须做一些导入工作:

import qualified Data.Tree as T
import Data.Tree.Pretty
我已经导入了Data.Tree,因为它还定义了一个数据构造函数节点。这意味着当我指的是导入的树时,我将使用T.Node,但当我指的是您的树时,只使用Node

它使用玫瑰树,每个节点可以有任意数量的子树:

data Tree a = Node {
        rootLabel :: a,         -- ^ label value
        subForest :: Forest a   -- ^ zero or more child trees
    }
并定义drawVerticalTree::Tree String->String,我将使用它。 我们需要做的就是将您的树转换为此树,然后我们就可以开展业务了:

toTree :: (Show k,Show d) => Tree k d -> T.Tree String
toTree Nil = T.Node "-" []
toTree (Node t1 k d t2) = T.Node (show k ++ ":" ++ show d) [toTree t1,toTree t2]
现在,让我们在IO monad中显示生成字符串并打印输出:

showtree :: (Show k, Show d) => Tree k d -> String
showtree = drawVerticalTree.toTree

printtree :: (Show k, Show d) => Tree k d -> IO ()
printtree = putStrLn.('\n':).showtree
这提供了很好的输出:

*Main> printtree example1

    7:"there"     
        |         
     ---------    
    /         \   
4:"Hello"  21:"hi"
    |         |   
    --        --  
   /  \      /  \ 
   -  -      -  - 
并相当优雅地处理了稍大的示例:

*Main> printtree example2

                   34:"well"                   
                       |                       
         ------------------------              
        /                        \             
    7:"there"                55:"This"         
        |                        |             
     ---------       -------------             
    /         \     /             \            
4:"Hello"  21:"hi"  -         102:"much"       
    |         |                   |            
    --        --            ------------       
   /  \      /  \          /            \      
   -  -      -  -      73:"one's"  132:"bigger"
                           |            |      
                           --           --     
                          /  \         /  \    
                          -  -         -  -    

那是因为你没有涵盖所有可能的案例。您将如何处理节点Nil k1 d1节点Nil k2 d2 Nil?我建议重新调整,这样就有了一个Nil和一个节点lk d r的情况,我不明白你说的重新调整是什么意思。你的意思是我应该在第二行有警卫来匹配不同的边缘情况吗?我的意思是你根本不应该处理不同的边缘情况:它们是你错误的根源。只治疗一般情况!写两行,从treeprint Nil=???树打印节点LKDR=???。然后找出每一个是什么???应该是的。我有一箱是零,一箱是节点LKDR。你能给我指一下右边吗direction@Jackwelch你能给我们举一个树的例子,其中treeprint给出了一个非穷举模式错误吗?那是因为你没有涵盖所有可能的情况。您将如何处理节点Nil k1 d1节点Nil k2 d2 Nil?我建议重新调整,这样就有了一个Nil和一个节点lk d r的情况,我不明白你说的重新调整是什么意思。你的意思是我应该在第二行有警卫来匹配不同的边缘情况吗?我的意思是你根本不应该处理不同的边缘情况:它们是你错误的根源。只治疗一般情况!写两行,从treeprint Nil=???树打印节点LKDR=???。然后找出每一个是什么???应该是的。我有一箱是零,一箱是节点LKDR。你能给我指一下右边吗direction@Jackwelch你能为我们辩护吗 举一个树的例子,其中treeprint给出了一个非穷举模式错误?