3 ms·
I got gpt-4-vision to transcribe the image from that tweet, looks like it's from [1], "Functional programming in OCAML". > This balance function can be written
by kristianp 3y ago
I got gpt-4-vision to transcribe the image from that tweet, looks like it's from [1], "Functional programming in OCAML".
> This balance function can be written simply and concisely using pattern matching, where each of the four input cases is mapped to the same output case. In addition, there is the case where the tree is left unchanged locally.
let balance = function
| Black, z, Node (Red, y, Node (Red, x, a, b), c), d
| Black, z, Node (Red, x, a, Node (Red, y, b, c)), d
| Black, x, a, Node (Red, z, Node (Red, y, b, c), d)
| Black, x, a, Node (Red, y, b, Node (Red, z, c, d)) ->
Node (Red, y, Node (Black, x, a, b), Node (Black, z, c, d))
| a, b, c, d -> Node (a, b, c, d)
val balance : color \* 'a \* 'a rbtree \* 'a rbtree -> 'a rbtree = <fun>
[1] https://courses.cs.cornell.edu/cs3110/2021sp/textbook/eff/rb.html https://courses.cs.cornell.edu/cs3110/2021sp/textbook/eff/rb...