RosettaCodeData/Task/Same-fringe/F-Sharp/same-fringe.fs
2026-04-30 12:34:36 -04:00

13 lines
679 B
FSharp

type 'a btree = Leaf of 'a | BTree of ('a btree * 'a btree)
let rec next = function
| [] -> None
| h :: t -> match h with Leaf x -> Some (x,t)
|BTree(a,b) -> next (a::b::t)
let samefringe t1 t2 =
let rec aux s1 s2 = match (next s1, next s2) with
None, None -> true
|None, _ | _, None -> false
|Some(a,b), Some(c,d) -> (a=c) && aux b d in aux [t1] [t2]
let u,v,w = BTree(Leaf 1, BTree(Leaf 2, Leaf 3)),BTree(BTree(Leaf 1, Leaf 2), Leaf 3),BTree(BTree(Leaf 3, Leaf 2), Leaf 1)
let check a b = printfn (if samefringe a b then "same" else "different")
check u v; check v u; check v w