Reputation: 284
I have a list of lists:
[[5,1,0,0,5,5,0,0],[0,0,1,4,2,0,6,1],[1,1,6,3,0,1,0,0],[1,5,0,0,0,1,1,6]]
and a string "wxyz"
I would like to have: 1)
w: 5 1 0 0 5 5 0 0
x: 0 0 1 4 2 0 6 1
y: 1 1 6 3 0 1 0 0
z: 1 5 0 0 0 1 1 6
I wrote:
f c xs = putStrLn (c : ':' : ' ' : concat (intersperse " " $ map show xs))
to write one line
and 2)
g xxs c = mapM_ (f c) xxs
How can I modify 2) to loop through string "wxyz" in order to have 1) ?
Upvotes: 2
Views: 593
Reputation: 18189
Instead of mapM_
, you can use zipWithM_
from Control.Monad
:
g xss cs = zipWithM_ f cs xss
or, if you change the order of arguments in either f
or g
to match, you can do it with less "points":
g = zipWithM_ f
Also, concat (intersperse " " ...)
is otherwise known as unwords ...
.
Upvotes: 9
Reputation: 144136
You can use zip
and uncurry
:
g xxs c = mapM_ (uncurry f) (zip xxs c)
Upvotes: 3