This preview shows pages 1–7. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: 1 PROGRAMMING IN HASKELL Chapter 5  List Comprehensions 2 Set Comprehensions In mathematics, the comprehension notation can be used to construct new sets from old sets. {x 2  x {1...5}} The set {1,4,9,16,25} of all numbers x 2 such that x is an element of the set {15}. 3 Lists Comprehensions In Haskell, a similar comprehension notation can be used to construct new lists from old lists. [x^2  x [1..5]] The list [1,4,9,16,25] of all numbers x^2 such that x is an element of the list [1..5]. 4 Note: The expression x [1..5] is called a generator , as it states how to generate values for x. Comprehensions can have multiple generators, separated by commas. For example: > [(x,y)  x [1,2,3], y [4,5]] [(1,4),(1,5),(2,4),(2,5),(3,4),(3,5)] 5 Changing the order of the generators changes the order of the elements in the final list: > [(x,y)  y [4,5], x [1,2,3]] [(1,4),(2,4),(3,4),(1,5),(2,5),(3,5)] Multiple generators are like nested loops , with later generators as more deeply nested loops whose variables change value more frequently. 6 > [(x,y)  y [4,5], x [1,2,3]] [(1,4),(2,4),(3,4),(1,5),(2,5),(3,5)] For example: x [1,2,3] is the last generator, so the value of the x component of each pair changes most frequently. 7...
View Full
Document
 Spring '10
 Valtorta

Click to edit the document details