在haskell中生成解析输入错误

时间:2016-11-07 01:10:20

标签: haskell functional-programming

以下代码序列在输入|上生成解析错误。 如果输入是叶节点,则使用value调用leafFunc 如果输入是树节点,则使用左子树,值,右子树

调用TreeFunc
data Tree t = Leaf t
        | Node (Tree t) t (Tree t)

foldTree :: (t1 -> t -> t1 -> t1) -> (t -> t1) -> Tree t -> t1
foldTree treeFn leafFn tree= | foldTree (Leaf v) = leafFn(v)
                             | foldTree (Node left v right) = treeFn(left v right)


Input : foldTree (\t1 t t2->t1 + 5*t + t2) (\x->x+9) (Leaf 5)
Expected Output : 14

Input : foldTree (\t1 t t2->t1 + 3*t + t2) (\x->x+5) (Tree (Leaf 3) 2 (Leaf 4))
Expected Output : 23

我是哈斯克尔的新手。

2 个答案:

答案 0 :(得分:1)

data Tree t = Leaf t
              | Node (Tree t) t (Tree t)

foldTree :: (Tree t -> t -> Tree t -> t1) -> (t -> t1) -> Tree t -> t1
foldTree treeFn leafFn (Leaf v) = leafFn v
foldTree treeFn leafFn (Node left v right) = treeFn left v right

答案 1 :(得分:1)

我猜这是你想要的

grep -v