Haskell Convert List to List of Tuples

空扰寡人 提交于 2019-12-05 05:49:31
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

Travis Brown

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.

Exactly for a task like this I find it convenient to have a stride function to take every n-th element from the list:

stride _ [] = []
stride n (x:xs) = x : stride n (drop (n-1) xs)

It can be used to convert a list to pairs:

toPairs xs = zip (stride 2 xs) (stride 2 (drop 1 xs))

An example (note that the last element may be thrown away if it doesn't have pair):

ghci> stride 2 [1..5]
[1,3,5]
ghci> toPairs [1..7]
[(1,2),(3,4),(5,6)]

It can be even easily extended to triplets or longer tuples:

toTriplets xs = zip3 as bs cs
  where as = stride 3 xs
        bs = stride 3 $ drop 1 xs
        cs = stride 3 $ drop 2 xs

To perform conversion from String to integer in your example, you can map read function over the second stride:

let lst = ["peter","1000","michell","2000","kelly","3000"] in
zip (stride 2 lst) (map read . stride 2 . drop 1 $ lst) :: [(String,Int)]

which gives:

[("peter",1000),("michell",2000),("kelly",3000)]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!