Using foldr function in ML to merge a list of lists

1k views Asked by At

If I have a set of lists such as [[1,2,4], [3], [0,9,8]...], and assuming routine 'merge' is predefined, how do I merge these lists into one single list using the foldr function?

The syntax for foldr is : foldr f init [...]; I'm confused what the initial condition should be in this case.

Any help would be appreciated, Thanks.

1

There are 1 answers

0
sshine On

Using foldr to achieve this, you would use foldr (fn (xs, result) => ...) initial xss where initial is one of the operands to merge (it works as the first result while the result of merging works as the subsequent results). That is, its type is a list, and it should have the property that merging it with the first of the sub-lists should result in only the first of the sub-lists. There is only one list that does this.