Ich habe versucht, jedem Element eines Baums eine Zahl zuzuweisen. Ich dachte, mit refs
würde die Aufgabe einfacher, aber ich stieß auf ein seltsames Verhalten: die Nummern zugewiesen waren nicht eindeutig und kein klares Muster entstand. Ich habe es geschafft, den Fehler zu beheben (Hinzufügen der let unboxed = !second_ref in
Zeile), aber ich verstehe nicht, was passiert ist.OCaml, unerwartetes Verhalten bei Refs und Bäumen
Der erste Baum in der Ausgabekonsole stellt nur sicher, dass die print_tree
Funktion ausgibt, was es sollte.
Die für den zweiten Druck erwartete Ausgabe sollte jedoch genau der gleiche sein wie der dritte Baum. Was vermisse ich ?
type ('a, 'b) tree =
| Node of 'a * ('a, 'b) tree * ('a, 'b) tree
| Leaf of 'b
let print_tree tree string_of_node string_of_leaf =
let rec print indent tree =
match tree with
| Leaf (l) -> print_string (indent^" -> "^string_of_leaf(l)^"\n")
| Node (n, left, right) ->
Printf.printf "%s-----------\n" indent;
print (indent^"| ") left;
Printf.printf "%s%s\n" indent (string_of_node(n));
print (indent^"| ") right;
Printf.printf "%s-----------\n" indent
in print "" tree
let myTree = Node(1,Node(2,Leaf(3),Leaf(4)),Node(5,Leaf(6),Leaf(7))) ;;
let first_ref = ref 0 ;;
let rec bug tree =
first_ref := !first_ref+ 1;
match tree with
|Leaf(a) -> Leaf(!first_ref)
|Node(n,l,r) -> Node(!first_ref, bug l, bug r) ;;
let second_ref = ref 0 ;;
let rec bug_fixed tree =
second_ref := !second_ref + 1;
let unboxed = !second_ref in
match tree with
|Leaf(a) -> Leaf(unboxed)
|Node(n,l,r) -> Node(unboxed, bug_fixed l, bug_fixed r) ;;
let bug_tree = bug myTree ;;
let bug_fixed_tree = bug_fixed myTree ;;
print_tree myTree string_of_int string_of_int ;
print_tree bug_tree string_of_int string_of_int ;
print_tree bug_fixed_tree string_of_int string_of_int ;
Der Ausgang ist die folgende:
-----------
| -----------
| | -> 3
| 2
| | -> 4
| -----------
1
| -----------
| | -> 6
| 5
| | -> 7
| -----------
-----------
-----------
| -----------
| | -> 7
| 7
| | -> 6
| -----------
7
| -----------
| | -> 4
| 4
| | -> 3
| -----------
-----------
-----------
| -----------
| | -> 7
| 5
| | -> 6
| -----------
1
| -----------
| | -> 4
| 2
| | -> 3
| -----------
-----------
Dies ist wahrscheinlich Wegthema hier, aber Ihre Definition des Typs 'tree' mir ein Rätsel. Die Blätter können einen anderen Typ als die Knoten haben? – RichouHunter