MrFabio
MrFabio

Reputation: 616

Parse elements from a List of Lists in Haskell

I have a list of a list with 3 strings like tst3, and I need to parse all the list and use the first and second arguments(strings) in function. But the map doesn't work well with it

  function a b = do {putStrLn (a ++ "stuff");
                     putStrLn b;}


  tst3 = [["aaa","aaaaaaaa","112121"],["asda","a22","aaax"]]
  fx2 s = map fx3 (tst3)
      where fx3 s = function (s!!0)(s!!1)



Couldn't match expected type `[a]' against inferred type `Char'
  Expected type: [[[a]]]
  Inferred type: [[Char]]
In the second argument of `map', namely `(tst3)'
In the expression: map fx3 (tst3)

Is there a better way to do this ? I can use [("aa","bb"),("ww","cc"),("jj","oooo")] if it gets easier

Thanks

Upvotes: 0

Views: 719

Answers (1)

leftaroundabout
leftaroundabout

Reputation: 120711

There's nothing wrong with what you've written there, however it's not actually very meaningful. You're transforming a list of some kind of data into a list of IO actions, but that alone doesn't do anything: what you probably want is perform these actions for every element of the list. This kind of thing is typically done with mapM_:

function :: String -> String -> IO()
function a b = do putStrLn (a ++ "stuff")   -- there's no reason to use curly
                  putStrLn b                -- brackets here


tst::[(String,String,String)]
tst3 = [("aaa","aaaaaaaa","112121"),("asda","a22","aaax")]

fx2 :: a -> IO()
fx2 s = mapM_ fx3 (tst3)      -- note that this s parameter is not used at all
    where fx3 (s,s',_) = function s s'

Upvotes: 3

Related Questions