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

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. HERE: 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.

On Tuesday 21 June 2011, 21:58:05, anthony niro 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.
HERE:
boomBangs xs = [ if x < 10 then "BOOM!" else "BANG!" | x <- xs, odd x]
That's a "list comprehension", if you want further information, searching for that term should turn up a few sources.
I dont understand this line except the "if" "then" "else". What is xs?
xs is the argument of the function boomBangs, it is a list. (mnemonic: xs is the plural of x, it's common to denote single elements of a list by x, y, k, n, ... and the corresponding lists of [potentially] several such elements xs, ys, ...)
what is the | ?
It's a separator. It separates the elements of the result list from the generators and predicates. List comprehension syntax is similar to set comprehension syntax, the above reads as (if x < 10 then "BOOM!" else "BANG!") where x runs through/is drawn from xs and x is odd.
and why doing this " | x <- xs, odd x]"
why x <- xs????? what is that
"x <- xs" is the 'generator', you could also read that as "for all x in xs" (but, as you will soon discover, it's more general, that works also for other things than lists).
and what is odd x?
That's a predicate or test, `odd' is a function which tests whether a number is odd, hence `odd x' tells you whether x is an odd number. Appearing in that position, it acts as a filter, the expression to the left of the separator (`|', to reiterate) is only included in the result list if the test evaluates to True. HTH, Daniel

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
back.
- "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
is.
- " | 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!
Tom
On 6/21/11, anthony niro
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.
HERE:
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.
participants (3)
-
anthony niro
-
Daniel Fischer
-
Tom Murphy