2014-02-08 78 views
0

考虑以下类型来表示玫瑰树:哈斯克尔:列表玫瑰树

data RTree a = No a [RTree a] 

考虑函数

tolist a = tolistAux 1 a 
    where tolistAux n (No x l) = (x,n) : (concat (map (tolistAux (n+1)) l)) 

我需要定义第一个函数的反函数:unlist :: [(a,Int)] -> RTree a

这样unlist (tolist a) = a

+0

这是如此的相似,你刚才的问题,我相信你会学会更多无需额外输入即可完成作业的这一步骤。请认真思考你之前的问题,以及为什么你的老师在此之前立即提问。 –

+0

@enoughreptocomment我提出了一个可能的解决方案。我怎么能在这里解决我以前的问题?有任何想法吗? – user3276667

回答

1

这里是解决方案我发现。我知道,如果在(a,b)中,b == 1,那么我创建一个No a(...) 因此,我有一个列表并将此列表分隔成几个以(a,b)开头的列表, 1)首先将b减1。 然后,我创建一个使用递归(即GA地图功能)树:

unlist ((x,1):t) = No x l3 
where l1 = map (\(a,b) -> (a,b-1)) t 
    l2 = isolate1 l1 
    l3 = map unlist l2 

isolate1 :: [(a,b)]->[[(a,b)]] --note that the result of isolate1 is a list of lists of pairs 
isolate1 [] = [] 
isolate [x]=[[x]] 
isolate1 ((a,b):t) = let (x:xs):ys = isolate1 t 
        in |b==1 = ((a,b):x:xs):ys 
         |otherwise = [(a,b)]:(x:xs):ys 

很高兴看到更多的解决方案:)