i have a list like this
["peter","1000","michell","2000","kelly","3000"]
and i would like to convert to
[("peter",1000),("michell", 2000),("kelly",3000)]
Please help. Thanks.
cnv :: [String] -> [(String, Integer)]
cnv [] = []
cnv (k:v:t) = (k, read v) : cnv t
If you want to handle odd-length just add cnv [x] =
variant before last one
ony's solution is a bit shorter, but here's a non-recursive version using splitEvery
from the very handy split
library:
cnv = map (\[name, amount] -> (name, read amount :: Int)) . splitEvery 2
The steps here are somewhat clearer (for me, at least) than in the recursive version.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With