我想用标准 ML 制作函数映射树
I want to make function maptree with standard ML
我想用标准 ML 制作函数映射树。
如果函数 f(x) = x + 1;
然后
maptree(f, NODE(NODE(LEAF 1,LEAF 2),LEAF 3));
应该做出结果
NODE(NODE(LEAF 2,LEAF 3),LEAF 4))
我写的代码如下。
datatype 'a tree = LEAF of 'a | NODE of 'a tree * 'a tree;
fun f(x) = x + 1;
fun maptree(f, NODE(X, Y)) = NODE(maptree(f, X), maptree(f, Y))
| maptree(f, LEAF(X)) = LEAF(f X);
但是当我像这样执行这段代码时
maptree(f, (NODE(NODE(LEAF 1,LEAF 2),LEAF 3)));
结果不是我想要的
(节点(节点(叶 2,叶 3),叶 4)))
但
节点(节点(叶子#,叶子#),叶子 4)。
为什么会这样(不是数字而是#)?
当 REPL 打印的数据结构比预设值更深时,REPL 使用 #
。如果你增加那个值,你会得到你例外的结果。我假设您使用的是 SML/NJ,它调用该设置 print.depth
:
sml -Cprint.depth=20
- maptree(f, (NODE(NODE(LEAF 1,LEAF 2),LEAF 3)));
val it = NODE (NODE (LEAF 2,LEAF 3),LEAF 4) : int tree
您可以通过执行 sml -H
找到更多类似的选项。在 "compiler print settings" 部分下查找它们:
compiler print settings:
print.depth (max print depth)
print.length (max print length)
print.string-depth (max string print depth)
print.intinf-depth (max IntInf.int print depth)
print.loop (print loop)
print.signatures (max signature expansion depth)
print.opens (print `open')
print.linewidth (line-width hint for pretty printer)
一些评论:
我可能会接受定义
datatype 'a tree = Leaf | Node of 'a tree * 'a * 'a tree
这样也可以表示零个或两个元素的树
我可能会咖喱树图功能
fun treemap f Leaf = Leaf
| treemap f (Node (l, x, r)) = Node (treemap f l, x, treemap f r)
因为您可以部分应用它,例如喜欢:
(* 'abstree t' returns t where all numbers are made positive *)
val abstree = treemap Int.abs
(* 'makeFullTree n' returns a full binary tree of size n *)
fun makeFullTree 0 = Leaf
| makeFullTree n =
let val subtree = makeFullTree (n-1)
in Node (subtree, n, subtree)
end
(* 'treetree t' makes an int tree into a tree of full trees! *)
val treetree = treemap makeFullTree
您有时可能也想 fold a tree。
我想用标准 ML 制作函数映射树。 如果函数 f(x) = x + 1; 然后
maptree(f, NODE(NODE(LEAF 1,LEAF 2),LEAF 3));
应该做出结果
NODE(NODE(LEAF 2,LEAF 3),LEAF 4))
我写的代码如下。
datatype 'a tree = LEAF of 'a | NODE of 'a tree * 'a tree;
fun f(x) = x + 1;
fun maptree(f, NODE(X, Y)) = NODE(maptree(f, X), maptree(f, Y))
| maptree(f, LEAF(X)) = LEAF(f X);
但是当我像这样执行这段代码时
maptree(f, (NODE(NODE(LEAF 1,LEAF 2),LEAF 3)));
结果不是我想要的 (节点(节点(叶 2,叶 3),叶 4))) 但 节点(节点(叶子#,叶子#),叶子 4)。 为什么会这样(不是数字而是#)?
#
。如果你增加那个值,你会得到你例外的结果。我假设您使用的是 SML/NJ,它调用该设置 print.depth
:
sml -Cprint.depth=20
- maptree(f, (NODE(NODE(LEAF 1,LEAF 2),LEAF 3)));
val it = NODE (NODE (LEAF 2,LEAF 3),LEAF 4) : int tree
您可以通过执行 sml -H
找到更多类似的选项。在 "compiler print settings" 部分下查找它们:
compiler print settings:
print.depth (max print depth)
print.length (max print length)
print.string-depth (max string print depth)
print.intinf-depth (max IntInf.int print depth)
print.loop (print loop)
print.signatures (max signature expansion depth)
print.opens (print `open')
print.linewidth (line-width hint for pretty printer)
一些评论:
我可能会接受定义
datatype 'a tree = Leaf | Node of 'a tree * 'a * 'a tree
这样也可以表示零个或两个元素的树
我可能会咖喱树图功能
fun treemap f Leaf = Leaf | treemap f (Node (l, x, r)) = Node (treemap f l, x, treemap f r)
因为您可以部分应用它,例如喜欢:
(* 'abstree t' returns t where all numbers are made positive *) val abstree = treemap Int.abs (* 'makeFullTree n' returns a full binary tree of size n *) fun makeFullTree 0 = Leaf | makeFullTree n = let val subtree = makeFullTree (n-1) in Node (subtree, n, subtree) end (* 'treetree t' makes an int tree into a tree of full trees! *) val treetree = treemap makeFullTree
您有时可能也想 fold a tree。