## Snippets tagged tail recursion

• ### Tree searching using Tail recursion with continuation

Sample code which demonstrate tree searching using : Tail recursion with continuation

15 people like this

Posted: 12 years ago by Ankur Dhama

• ### Remove duplicate list elements

I have been working on my "Functional Style" and like to think that I have improved a bit. The list order is no longer maintained in this snippet; however as the order change is simply a feature of the foldBack process the reversal of input and reversal of output is a reliable (and efficient?) fix.

4 people like this

Posted: 10 years ago by visProcessEngg

• ### Combinator for tail-recursive functions

The snippet defines a combinator 'tailrec' that can be used to express tail-recursive functions. If you use 'tailrec' and do not mark your function as recursive, then the function will be a tail-recursive one.

1 people like this

Posted: 9 years ago by Tomas Petricek

• ### sum the nodes in a (not-binary) tree using continuations

you can easily find how to use continuations to iterate over a binary tree but what if the count of children for each node is not known at design time? It's not so obvious how to do this in order to get a tail-recursive method. This short snippet shows how to do this to sum the values of every leaf. The second part demonstrates a general approach for other operations than addition.

26 people like this

Posted: 11 years ago by Carsten König

• ### Factorial

Factorial versus tail recursion

4 people like this

Posted: 10 years ago by Laco

• ### Rotate or shift lists

This rotates or shifts a list. Unlike http://www.fssnip.net/qY/title/Rotate-List, which runs exponentially, this runs at O(n). In case of overflow (i.e., shift index is larger than the size of the list) will run at most once more with the modulo. This is done to prevent using `List.length` prior to entering the function, as that would lead to a perf punishment of an extra O(n) on each invocation. For large lists, `shift largeList 1` would then get a big performance hit.

1 people like this

Posted: 5 months ago by Abel Braaksma