
Tom Pledger wrote:
Toby Watson writes: | Intuitively the following scenarios seem to be related, can anyone | point my in the direction of formal work on this, or give me the | formal terms I need to search around? | | 1. Adding two integers together: Int -> Int -> Int | | 2. Adding two lists of Integers together: [Int] -> [Int] -> [Int]
If you're adding each element of the first list to the element in the corresponding position in the second list, it's usually called zipping. The zipWith function in the prelude takes as its parameter a function of type a->b->c (your scenario 1) and returns a function of type [a]->[b]->[c] (your scenario 2).
Jeff Lewis implemented zip comprehensions in the latest versions of both GHC and Hugs. This notation provides special syntax to deal with the second case above nicely. You can say: [x + y | x <- [1,2,3,4,5] | y <- [5,6,7,8]] to get: [6,8,10,12] The shorther list determines the final length (could of course be infinite.) It's an extension of the usual syntax, so multiple generators are OK too: [x + y | x <- [1,2,3,4,5], z <- [3, 4, 5], (x+z) `mod`2 == 1 | y <- [5,6,7,8], y `mod` 2 == 0] gives: [7, 10] It's a great notation that avoids zipWith's. (You need to start hugs with -98 and GHC needs -fglasgow-exts for zip comprehensions to be recognized.) -Levent.