Coincidentally or not, this very problem was
posted today. I offered
a recursive solution in Perl that (it appears to me) corresponds reasonably well to your ML one.
After reading the link you provided, I figured out that the problem is somewhat different: it's supposed to yield only one result. I offer a solution that parallels the ML one in another post in this thread.
Caution: Contents may have been coded under pressure.