Query on list comprehension

What are the limitations of list comprehension. I want to use listcomprehension to output the pattern below. So a mixture of a's and newline characters. The part im stuck at is creating arguments in the listcomprehension to stop at some point then execute next loop. Is it even possible to create the pattern below purely from list comprehension.Thankyou in advance. a aa aaa -- View this message in context: http://www.nabble.com/Query-on-list-comprehension-tp22573574p22573574.html Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

Use a nested list comprehension, for example Prelude> [['a'| i <- [0..n]]++"\n"|n <- [1..3]] ["aa\n","aaa\n","aaaa\n"] Am 18.03.2009 um 07:47 schrieb Melanie_Green:
What are the limitations of list comprehension. I want to use listcomprehension to output the pattern below. So a mixture of a's and newline characters. The part im stuck at is creating arguments in the listcomprehension to stop at some point then execute next loop. Is it even possible to create the pattern below purely from list comprehension.Thankyou in advance.
a aa aaa -- View this message in context: http://www.nabble.com/Query-on-list- comprehension-tp22573574p22573574.html Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Melanie_Green
What are the limitations of list comprehension. I want to use listcomprehension to output the pattern below. So a mixture of a's and newline characters. The part im stuck at is creating arguments in the listcomprehension to stop at some point then execute next loop. Is it even possible to create the pattern below purely from list comprehension.Thankyou in advance.
a aa aaa
I'm not clear what you mean by the question. Why do you want to use list comprehensions? What if they aren't the best way of getting the result you want? You can write [a | b <- [replicate n 'a' | n <- [1..]], a <- b ++ "\n"] but does that "replicate" fail to meet your specification? If not, you can replace it with another list comprehension like this: [a | b <- [['a'| m <- [1..n]] | n <- [1..]], a <- b ++ "\n"] but at this point, comprehension is not what most people would get from reading the code. I'd say it was clearer to write concat [replicate n 'a' ++ "\n" | n <- [1..]] -- Jón Fairbairn Jon.Fairbairn@cl.cam.ac.uk http://www.chaos.org.uk/~jf/Stuff-I-dont-want.html (updated 2009-01-31)

Melanie_Green writes:
I want to use listcomprehension to output the pattern below...
Jón Fairbairn wrote:
Why do you want to use list comprehensions? What if they aren't the best way of getting the result you want?
unlines . tail . inits . repeat $ 'a'
concat [replicate n 'a' ++ "\n" | n <- [1..]]
Or: unlines [replicate n 'a' | n <- [1..]] -Yitz

On Tue, 17 Mar 2009, Melanie_Green wrote:
What are the limitations of list comprehension. I want to use listcomprehension to output the pattern below. So a mixture of a's and newline characters. The part im stuck at is creating arguments in the listcomprehension to stop at some point then execute next loop. Is it even possible to create the pattern below purely from list comprehension.Thankyou in advance.
a aa aaa
iterate ('a':) "\n"

Henning Thielemann
On Tue, 17 Mar 2009, Melanie_Green wrote:
What are the limitations of list comprehension. I want to use listcomprehension to output the pattern below. So a mixture of a's and newline characters. The part im stuck at is creating arguments in the listcomprehension to stop at some point then execute next loop. Is it even possible to create the pattern below purely from list comprehension.Thankyou in advance.
a aa aaa
iterate ('a':) "\n"
Game to Henning Thielemann! -- Jón Fairbairn Jon.Fairbairn@cl.cam.ac.uk http://www.chaos.org.uk/~jf/Stuff-I-dont-want.html (updated 2009-01-31)

The problem we were asked about was specifically a aa aaa The code (iterate ('a':) "\n") does not give the right answer. It's not just that it produces an infinite list instead of three strings, it doesn't even start with the right string. It starts with "\n" when we need "a\n". To produce the specified output using that pattern, you need (take 3 . tail . iterate ('a':)) "\n" or any of several other alternatives. The original poster also didn't ask "what is the best way to do this", but specifically asked about doing it with list comprehension. Presumably this was an attempt to understand list comprehension better.

"Richard O'Keefe"
The problem we were asked about was specifically a aa aaa The code (iterate ('a':) "\n") does not give the right answer. It's not just that it produces an infinite list instead of three strings, it doesn't even start with the right string. It starts with "\n" when we need "a\n".
It was impossible to determine that from the question.
To produce the specified output using that pattern, you need (take 3 . tail . iterate ('a':)) "\n"
take 9, surely?
or any of several other alternatives.
The original poster also didn't ask "what is the best way to do this", but specifically asked about doing it with list comprehension. Presumably this was an attempt to understand list comprehension better.
Perhaps, but as the OP didn't follow up to the message where I said that it wasn't clear what the question was, by the time Henning posted, I think he was justified in generalising the question and taking the answer further. This café is for discussion; it's not a suitable place for asking a question, copying out the answer and disappearing without further comment. -- Jón Fairbairn Jon.Fairbairn@cl.cam.ac.uk
participants (6)
-
Adrian Neumann
-
Henning Thielemann
-
Jon Fairbairn
-
Melanie_Green
-
Richard O'Keefe
-
Yitzchak Gale