This is from the book I'm reading:

We’ll start with an example. Suppose we have a list L:Two immediate observations:1> L = [1,2,3,4,5].And suppose we want to double every element in the list. We’ve done this before, but I’ll remind you:

[1,2,3,4,5]2> lists:map(fun(X) -> 2*X end, L).But there’s a much easier way that uses a list comprehension:

[2,4,6,8,10]4> [2*X || X <- L ].The notation [ F(X) || X <- L] means “the list of F(X) where X is taken from the list L.” Thus, [2*X || X <- L ] means “the list of 2*X where X is taken from the list L.”

[2,4,6,8,10]

- Python, this ain't.
- My god, you have to read it right-to-left to follow the logic. Who thought that was a good idea?

Leaving for British Columbia tomorrow to spend the holidays with my wife's extended family (sharing a small house with two infants included). Wish me lots of patience. ;)

## 3 comments:

Does reading right to left mean you need to take up japanese, too? Enjoy BC, take pictures if you get a chance. Some of us poor americans would like to see the exotic lands of our rich neighbors to the north.

Compare to common mathematical notation for sets:

{ 2*x | x \in L }

doesn't seem to be too far off, if you ignore the annoying shuffling of all symbols...

Huh?

[2*X for X in L]

The symbols may be different, but the similarities are there.

Until you get to message-passing, of course.

Post a Comment