[Haskell-beginners] reorganizing lists

Bryce Verdier bryceverdier at gmail.com
Mon Jan 28 22:50:34 CET 2013


On 01/28/2013 11:32 AM, Martin Drautzburg wrote:
> On Monday, 28. January 2013 19:37:53 Bryce Verdier wrote:
>> Hi All,
>>
>> At the moment I have a list of lists. The inner list is a coordinate,
>> like (x,y) but is [x,y] instead. What I would like to do is group all
>> the x's into one list, and the y's into another. I know I can do this
>> with calling 2 maps on the container, but I would also like to do this
>> in one iteration.
> Something like this?
> 	groupMe  = foldl (\[rx,ry] [x,y] -> [x:rx,y:ry]) [[],[]]
>
>
> *Main> groupMe [[1,2],[1,3],[2,3]]
> [[2,1,1],[3,3,2]]
Thank you all for your responses. This is what I ultimately went with 
(pasted below). If anyone would like to share a way to improve this 
(because I know it can be), please share. I'm still learning. :)

module Main where

import qualified Data.Text as T
import qualified Data.Text.IO as TI (readFile)
import Data.List (transpose)
import Numeric.GSL.Fitting.Linear (linear)
import Data.Packed.Vector (fromList)

buildList :: IO [[T.Text]]
buildList = TI.readFile "lin_reg_data.txt" >>= return . transpose . map 
(T.split (=='_')) . T.lines

main :: IO ()
main = do
     values <- buildList
     let values2 = map (fromList . map (\x -> read (T.unpack x):: 
Double)) values
     print [linear (head values2) (last values2)]



Warm regards,
Bryce



More information about the Beginners mailing list