geniusfat wrote:
hi dear haskell lover ;)
what I want to do is simply this:
select3 :: [a] -> [(a, a, a)]
and how can it be done efficiently?
thanks in advance!


If, given [1,2,3,4,5,6,7,8,9,10,11,12] you want [(1,2,3),(4,5,6),(7,8,9)....] then:

map (take 3) . iterate (drop 3)

is very nearly what you need.

Two problems: (a) it gives you [[1,2,3],[4,5,6]..] instead
(b) it carries on with an infinite number of [] empty lists

you can fix both of these:

map (\[a,b,c]->(a,b,c)) . takeWhile (not.null) . map (take 3) . iterate (drop 3)

Prelude> map (\[a,b,c] -> (a,b,c)) . takeWhile (not.null) . map (take 3) . iterate (drop 3) $ [1..12]
[(1,2,3),(4,5,6),(7,8,9),(10,11,12)]


Hope that helps.

Jules

_______________________________________________
Haskell-Cafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to