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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.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,我正在努力解决这个例子: 编写将函数折叠到树上的折叠函数。因此 例如,foldmint将找到树中最小的元素 fold :: (a -> a -> a) -> Tree -> a *那是我的剧本 data Tree = Leaf Int | Fork Tree Int Tree deriving Show t0 = Leaf 0 t1 = Fork (Fork (Leaf 1) 2 (Leaf 3)) 4 (Fork (Leaf 5) 6

我正在努力解决这个例子:

编写将函数折叠到树上的折叠函数。因此 例如,foldmint将找到树中最小的元素

fold :: (a -> a -> a) -> Tree -> a
*那是我的剧本

data Tree = Leaf Int
          | Fork Tree Int Tree
     deriving Show


t0 = Leaf 0
t1 = Fork (Fork (Leaf 1) 2 (Leaf 3)) 4 (Fork (Leaf 5) 6 (Leaf 7))
t2 = Fork (Fork (Fork (Leaf 1) 2 (Leaf 3)) 4 (Leaf 5)) 6 (Leaf 7)

fold :: (a -> a -> a) -> Tree -> a
fold f v (Leaf n) = v
fold f v (Fork l n r) = f (Folk l) (fold f v (Fork r))

谢谢你的建议,有一件事,在你的函数fold的签名中,你说它将接收2个参数,一个二进制函数可以是一个操作符,一个树,并返回一个a。现在,在定义中有3个参数,f,一些v和一个树的构造函数

解决方案可以是:

fold :: (Int -> Int -> Int) -> Tree -> Int
fold f (Leaf n) = n
fold f (Fork l n r) = f n m
    where
        m = f (fold f l) (fold f r)
你需要说你正在返回一个Int,因为你的树包含Int

编辑

此外,还可以使用刚性类型变量重新定义数据

data Tree a = Leaf a
            | Fork (Tree a) a (Tree a)
     deriving Show


fold :: (a -> a -> a) -> Tree a -> a
fold f (Leaf n) = n
fold f (Fork l n r) = f n m
    where
        m = f (fold f l) (fold f r)

我建议您阅读的文件。他们有一个树数据类型的例子。

一件事,在函数fold的签名中,你说它将接收2个参数,一个二进制函数可以是一个运算符,一个树,并返回一个a。现在,在定义中有3个参数,f,一些v和一个树的构造函数

解决方案可以是:

fold :: (Int -> Int -> Int) -> Tree -> Int
fold f (Leaf n) = n
fold f (Fork l n r) = f n m
    where
        m = f (fold f l) (fold f r)
你需要说你正在返回一个Int,因为你的树包含Int

编辑

此外,还可以使用刚性类型变量重新定义数据

data Tree a = Leaf a
            | Fork (Tree a) a (Tree a)
     deriving Show


fold :: (a -> a -> a) -> Tree a -> a
fold f (Leaf n) = n
fold f (Fork l n r) = f n m
    where
        m = f (fold f l) (fold f r)

我建议您阅读的文件。它们有一个树数据类型的示例。

折叠可以从任何类型机械地派生出来。下面是民俗:

折叠数据类型意味着我们将提供一种方法,将该类型的每个可能构造函数组合成最终的类型r。这里,Maybe有两个构造函数:一个具有0个值,另一个具有类型为a的单个值。所以,要折叠它,我们需要为Nothing情况提供一个值,以及一个将Just a转换为r的函数

让我们看看这个列表,看看foldr是如何从中派生出来的

data List a = Nil | Cons a (List a)

           [1]   [2    3    4]             [4]
foldList :: r -> (a -> r -> r) -> List a -> r
这是替换列表中的Nil时要使用的值。 这是用于组合Cons案例的函数。因为Cons的第一个值是a,所以函数必须取a。 Cons取的第二个值是列表a,那么为什么取r呢?好我们正在定义一个函数,它可以获取一个列表a并返回一个r,因此我们将在与当前节点组合之前处理列表的其余部分。 fold函数返回r。 实现是什么样子的

foldList nil cons list = case list of
    Nil -> nil
    Cons a as -> cons a (foldList nil cons list)
所以我们用它们的析构函数替换所有的构造函数,必要时递归

对于树木:

data Tree = Leaf Int | Fork Tree Int Tree
我们有两个构造函数:一个接受Int,另一个接受三个参数Tree,Int和Tree

让我们来写签名

treeFold :: LeafCase r -> ForkCase r -> r
啊,但我们必须定义LeafCase:

由于Leaf使用单个Int作为参数,因此该函数的类型必须是这样的

type ForkCase r = r -> Int -> r -> r
同样,我们用r类型参数替换Fork中Tree的所有递归用法。因此,我们的全部功能如下所示:

foldTree :: (Int -> r) -> (r -> Int -> r -> r) -> Tree -> r
实施的开始可能是:

foldTree leaf fork tree = case tree of
    Leaf i -> error "halp"
    Fork l i r -> error "complete me?"

我相信你可以填补这些漏洞:

褶皱可以从任何类型机械衍生出来。下面是民俗:

折叠数据类型意味着我们将提供一种方法,将该类型的每个可能构造函数组合成最终的类型r。这里,Maybe有两个构造函数:一个具有0个值,另一个具有类型为a的单个值。所以,要折叠它,我们需要为Nothing情况提供一个值,以及一个将Just a转换为r的函数

让我们看看这个列表,看看foldr是如何从中派生出来的

data List a = Nil | Cons a (List a)

           [1]   [2    3    4]             [4]
foldList :: r -> (a -> r -> r) -> List a -> r
这是替换列表中的Nil时要使用的值。 这是用于组合Cons案例的函数。因为Cons的第一个值是a,所以函数必须取a。 Cons取的第二个值是列表a,那么为什么取r呢?好我们正在定义一个函数,它可以获取一个列表a并返回一个r,因此我们将在与当前节点组合之前处理列表的其余部分。 fold函数返回r。 实现是什么样子的

foldList nil cons list = case list of
    Nil -> nil
    Cons a as -> cons a (foldList nil cons list)
所以我们用它们的析构函数替换所有的构造函数,必要时递归

对于树木:

data Tree = Leaf Int | Fork Tree Int Tree
我们有两个构造函数:一个接受Int,另一个接受三个参数Tree,Int和Tree

让我们来写签名

treeFold :: LeafCase r -> ForkCase r -> r
啊,但我们必须定义LeafCase:

由于Leaf使用单个Int作为参数,因此该函数的类型必须是这样的

type ForkCase r = r -> Int -> r -> r
同样,我们用r类型参数替换Fork中Tree的所有递归用法。因此,我们的全部功能如下所示:

foldTree :: (Int -> r) -> (r -> Int -> r -> r) -> Tree -> r
实施的开始可能是:

foldTree leaf fork tree = case tree of
    Leaf i -> error "halp"
    Fork l i r -> error "complete me?"

我相信你能填补这些漏洞:

你写了什么?您得到了什么错误?数据类型树是什么样子的?很明显,它应该是一棵树,这样它就可以在树叶中储存任何类型的东西。这就是我做的。你写了什么?您得到了什么错误?数据类型树是什么样子的?很明显,它应该是一棵树,这样它才能储存东西
叶子上的任何a型。这就是我做的很好的回答。然而,我认为最后的数据。可折叠的建议误导。数据的折叠。折叠是指将数据类型展平到一个列表中,然后折叠到该列表中。它不像上面的折叠:a->a->a->树a->a,所以它很容易引起混淆。回答得好。然而,我认为最后的数据。可折叠的建议误导。数据的折叠。折叠是指将数据类型展平到一个列表中,然后折叠到该列表中。它不像上面的fold::a->a->a->Tree a->a那样是一般的亚同态,所以它很容易引起混淆。