In Haskell, how can you sort a list of infinite lists of strings?

Posted by HaskellNoob on Stack Overflow See other posts from Stack Overflow or by HaskellNoob
Published on 2010-03-07T00:47:28Z Indexed on 2010/05/15 2:54 UTC
Read the original article Hit count: 286

Filed under:
|
|
|
|

So basically, if I have a (finite or infinite) list of (finite or infinite) lists of strings, is it possible to sort the list by length first and then by lexicographic order, excluding duplicates? A sample input/output would be:

Input:

[["a", "b",...], ["a", "aa", "aaa"], ["b", "bb", "bbb",...], ...]

Output:

["a", "b", "aa", "bb", "aaa", "bbb", ...]

I know that the input list is not a valid haskell expression but suppose that there is an input like that. I tried using merge algorithm but it tends to hang on the inputs that I give it. Can somebody explain and show a decent sorting function that can do this? If there isn't any function like that, can you explain why?

In case somebody didn't understand what I meant by the sorting order, I meant that shortest length strings are sorted first AND if one or more strings are of same length then they are sorted using < operator.

Thanks!

© Stack Overflow or respective owner

Related posts about haskell

Related posts about sorting