Function that produces \$a^nb^nc^n\$ I'm trying to write a function that produces an infinite list containing strings of the form \$a^nb^nc^n\$.

My first idea was to do it using comprehensions.

f = [replicate n 'a' ++ replicate n 'b' ++ replicate n 'c' | n &lt;-[0..]]

But this does a lot of duplicate work. I then decided it would be better to do the following

import Data.List

f = "" : (abc "")

abc s = string : (abc string)
        where string = sort $ "abc" ++ s 


How can I do this more efficiently without having to sort and use ++ as frequently? Would changing to ByteStrings help?
Your first solution could be more elegantly written as:

f :: [String]
f = [concatMap (replicate n) "abc" | n &lt;- [0..]]


The basic operating principle would be the same, though.

A general sorting algorithm would be O(n log n) at best, so I don't believe that sort would be a good idea.