列表中的错误到树中 [英] Error in list to tree
本文介绍了列表中的错误到树中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
考虑以下类型:
数据LTree a = Leaf a |叉(LTree a)(LTree a)
build :: [(a,Int)] - > LTree a
build l = fst(buildaccum 0 l)e
我有一个列表,想要构建一棵树
buildaccum :: Int - > [(a,Int)] - > (LTree a,[(a,Int)])
buildaccum nl @((a,b):t)| n == b =(Leaf a,t)
| n (d,l2)= buildaccum(n + 2)l1
在ghci中,我得到以下错误:
无法与实际类型LTree匹配预期类型(LTree a,[(a,Int)])'a'
您是否可以发现错误?
解决方案
buildAccum
必须返回一个对类型(LTree a,[(a,Int)])
,但在第一个守护语句中返回一个原始LTree a
:Leaf a
。Consider the following type:
data LTree a = Leaf a | Fork (LTree a) (LTree a) build :: [(a,Int)] -> LTree a build l = fst (buildaccum 0 l)e
I have a list and want to build a tree
buildaccum :: Int -> [(a,Int)] -> (LTree a, [(a,Int)]) buildaccum n l@((a,b):t) |n==b = (Leaf a,t) |n<b = (Fork e d, l2) where (e,l1) = buildaccum (n+1) l (d,l2) = buildaccum (n+2) l1
In ghci, I get the following error:
Couldn't match expected type (LTree a, [(a, Int)])' with actual type LTree a'
Can you spot the error, please?
解决方案
buildAccum
must return a pair type(LTree a, [(a, Int)])
, but in your first guarded statement you return a rawLTree a
:Leaf a
.这篇关于列表中的错误到树中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文