Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.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
List 哈斯克尔:玫瑰树列表_List_Haskell_Tree_Higher Order Functions - Fatal编程技术网

List 哈斯克尔:玫瑰树列表

List 哈斯克尔:玫瑰树列表,list,haskell,tree,higher-order-functions,List,Haskell,Tree,Higher Order Functions,考虑以下类型来表示玫瑰树: 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)]->rtreea 这样unlist(tolist a)=a这是我找到的解决方案 我意识到如果在(a,b)中,b==1,那么我创建

考虑以下类型来表示玫瑰树:

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)]->rtreea


这样
unlist(tolist a)=a

这是我找到的解决方案

我意识到如果在(a,b)中,b==1,那么我创建了一个No a(…) 因此,我有一个列表,并通过先减去1到b,将这个列表分离成几个以(a,1)开头的列表。 然后,我使用递归(例如映射函数)创建树:


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

这与您的上一个问题非常相似,我相信您可以通过完成作业的这一步而不需要额外输入来了解更多内容。请仔细思考你的上一个问题,以及为什么你的导师在这个问题之前问这个问题。@n我给出了一个可能的解决方案。我怎样才能重新适应我前面问题的解决方案?有什么想法吗?
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