For example, if a list
has employees like ["Hari Krishna", "PTR",
"Nayan", "Sudhir"], then output should be like [(1,
"Hari Krishna"), (2, "PTR"), (3, "Nayan"), (4,
"Sudhir")].
ListUtil.hs
getNumbers :: Int -> Int -> [Int] getNumbers x y | (x == y) = [] | (x > y) = getNumbers' x y (-1) | otherwise = getNumbers' x y 1 getNumbers' :: Int -> Int -> Int -> [Int] getNumbers' x y increment | (x == y) = [y] | otherwise = x : (getNumbers' (x+increment) y increment) getIndexes :: [a] -> [(Int, a)] getIndexes [] = [] getIndexes xs = getIndexes' xs (getNumbers 1 (length xs)) getIndexes' :: [a] -> [Int] -> [(Int, a)] getIndexes' [] _ = [] getIndexes' (x:xs) (y:ys) = (y, x) : (getIndexes' xs ys)
*Main> :load ListUtil.hs [1 of 1] Compiling Main ( ListUtil.hs, interpreted ) Ok, modules loaded: Main. *Main> *Main> getIndexes ["Hari Krishna", "PTR", "Nayan", "Sudhir"] [(1,"Hari Krishna"),(2,"PTR"),(3,"Nayan"),(4,"Sudhir")] *Main>
No comments:
Post a Comment