
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