Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Without busting out CPS, you can also pass the current state along as parameters to your recursive calls:

    def fib(n, acc1 = 0, acc2 = 1) =
      n match
        case 0 => 0
        case 1 => acc2
        case _ => fib(n-1, acc2, acc1+acc2)
You should be able to take my tree walker here[0], add a `visit: A => ()` parameter, and call `visit(tree.value)` (assuming you have a structure Tree[A] = (value: A, children: List[Tree[A]])) before the match.

[0] https://news.ycombinator.com/item?id=43365880



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: