Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pretty Printing list of lists

Tags:

haskell

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) ?

like image 384
user3166747 Avatar asked Jan 28 '26 16:01

user3166747


1 Answers

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 ....

like image 98
Ørjan Johansen Avatar answered Jan 31 '26 08:01

Ørjan Johansen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!