[Haskell-beginners] boomBangs xs = [ if x < 10 then "BOOM!" else "BANG!" | x <- xs, odd x]

Tom Murphy amindfv at gmail.com
Tue Jun 21 22:26:56 CEST 2011

Hi Anthony,
     What you're looking at is called a list comprehension. It's a way
of describing a list, to be created.

     It looks like you're doing the LYAH tutorial. It will explain
list comprehensions in general, but here's a start:

- xs is the list that your new list "comes from." boomBang is a
function. You give it a list (xs), and it will give you a new list
- "x <- xs" is the way of saying that each element (called x) of your
new list comes form xs.
- the "|" separates the "sides" of the list comprehension. The left
side (the if statement) describes what each element of the new list
- "  | x <- xs, odd x]" is doing two things: getting individual
elements from xs, and making sure that each element is odd. If "odd x"
(for some x) doesn't equal True, then that x never "makes it" to the
left hand side (the if statement)

Good luck with your studies!

On 6/21/11, anthony niro <proant88 at hotmail.com> wrote:
> Hello,
> My name is Anthony i have few question about Haskell.
> I was reading a tutorial everything goes well. but now i have few things
> that i dont understang.
> boomBangs xs = [ if x < 10 then "BOOM!" else "BANG!" | x <- xs, odd x]
> I dont understand this line except the "if" "then" "else".
> What is xs?
> what is the |  ?
>  and why doing this  "  | x <- xs, odd x]"
> why x <- xs????? what is that
> and what is odd x?
> thx everyone for answer me.

More information about the Beginners mailing list