xxxxxxxxxx
type tree = Leaf | Node(int, tree, tree);
let rec sum = (item) => {
switch (item) {
| Leaf => 0
| Node(value, left, right) => value + sum(left) + sum(right);
}
};
let myTree =
Node(
1,
Node(2, Node(4, Leaf, Leaf), Node(6, Leaf, Leaf)),
Node(3, Node(5, Leaf, Leaf), Node(7, Leaf, Leaf))
);
Js.log(sum(myTree));
xxxxxxxxxx
// Generated by BUCKLESCRIPT, PLEASE EDIT WITH CARE
'use strict';
function sum(item) {
if (item) {
return (item[0] + sum(item[1]) | 0) + sum(item[2]) | 0;
} else {
return 0;
}
}
var myTree = /* Node */[
1,
/* Node */[
2,
/* Node */[
4,
/* Leaf */0,
/* Leaf */0
],
/* Node */[
6,
/* Leaf */0,
/* Leaf */0
]
],
/* Node */[
3,
/* Node */[
5,
/* Leaf */0,
/* Leaf */0
],
/* Node */[
7,
/* Leaf */0,
/* Leaf */0
]
]
];
console.log(sum(myTree));
exports.sum = sum;
exports.myTree = myTree;
/* Not a pure module */
xxxxxxxxxx
type tree =
| Leaf
| Node of int* tree* tree
let rec sum item =
match item with
| Leaf -> 0
| ((Node (value,left,right))[@explicit_arity ]) ->
(value + (sum left)) + (sum right)
let myTree =
((Node
(1,
((Node
(2, ((Node (4, Leaf, Leaf))[@explicit_arity ]),
((Node (6, Leaf, Leaf))[@explicit_arity ])))[@explicit_arity ]),
((Node
(3, ((Node (5, Leaf, Leaf))[@explicit_arity ]),
((Node (7, Leaf, Leaf))[@explicit_arity ])))[@explicit_arity ])))
[@explicit_arity ])
let _ = Js.log (sum myTree)
This div has the ID preview
.
Feel free to override its content, or choose "React Greetings" in the Examples menu!