Sample code which demonstrate tree searching using : Tail recursion with continuation
16 people like thisPosted: 13 years ago by Ankur Dhama
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 thisPosted: 12 years ago by visProcessEngg
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 thisPosted: 11 years ago by Tomas Petricek
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 thisPosted: 13 years ago by Carsten König
Factorial versus tail recursion
4 people like thisPosted: 11 years ago by Laco
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 thisPosted: 2 years ago by Abel Braaksma