Is there a way to write anonymous function recursive in Scala? I am thinking like this:
((tree: tree) => gt; print (t.value); for (c & lt; t children) this Method (c)}} (root) (related question :)
As you have mentioned in the posted link, you can use Y coordinator here. Here is an example:
Scala> F = OK [A, B] (F: (A = & gt; B) => (A = & gt; B)): A => B = F (OK (F)) (_) Fine: [A, B ] (F: (A) => B) => (A) => B) (A) = Gt; A => if (a & lt; = 0) 1 and F (A-1) * A) Fact: (Int) => int = & lt; function1 & gt; Scala> Fact (12) res0: Int = 479001600 Note that this does not work with large numbers Be careful with the tail call optimization.
Comments
Post a Comment