Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simplifying an ugly function in Haskell

I have defined the function f1

f1 p = foldl (\x y -> x ++ y ++ "\t") "" (map (foldl (++) "") p)

that will take

[["4","0","1"],["5","2","3"]]

and yield

"401\t523\t"

but is a function as ugly as it can get. I am sure there is a simpler way to implement the same function. Can anybody give me some clue about it?

like image 418
devoured elysium Avatar asked Sep 04 '26 03:09

devoured elysium


1 Answers

Function composition is your friend. So is intercalate, from Data.List

f1 = intercalate "\t" . map concat

Edit: whoops, misread your output. You want "\t" at the end of all of them, not just between them. In that case, it's closer to

f1 = concat . map ((++ "\t") . concat)
like image 82
Carl Avatar answered Sep 06 '26 12:09

Carl



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!