I know that a recursive function is a powerful method in F #. My question is: is there an exit instruction that can jump out of recursive functions, just like imperative languages. For example, insert node into a binary tree.
type Tree<'a> when 'a :> IComparable<'a> = | Nil | Leaf of 'a | Node of Tree<'a> * 'a * Tree<'a> let tt2 = Node( Node(Leaf "D", "B",Node(Leaf "G", "E", Leaf "H" )), "A", Node(Nil, "C", Node(Nil, "F", Leaf "I"))) let rec contains (x : #IComparable<'a>) = function | Nil -> false | Leaf y -> if x.CompareTo(y) = 0 then true else false | Node(l, y, r) -> match l, y, r with | l, y, Nil -> if x.CompareTo(y) = 0 then true else contains xl | Nil,y, r -> if x.CompareTo(y) = 0 then true else contains xr | _ -> if x.CompareTo(y) = 0 then true else contains xr |>ignore contains xl let xx = contains "C" tt2 //It is wrong answer.
source share