Advertisement
VladNitu

W2L1(Lecture3)-FP24-Vlad-Batch2Q5-WL

Feb 18th, 2024
1,485
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. import Data.List
  3.  
  4. reindex :: (Int -> Int) -> [a] -> [a]
  5. reindex f xs = map fst (sortBy sortFunction elements_indexes_after_f)
  6.   where
  7.     elements_indexes = zip xs [0 .. length xs - 1]
  8.     elements_indexes_after_f = map (\(x, id) -> (x, f id)) elements_indexes
  9.     sortFunction x y
  10.       | snd x < snd y = LT
  11.       | snd x > snd y = GT
  12.       | otherwise = EQ
  13.     -- (Shorter) alternative: sortFunction = \(_, a)  (_, b) -> compare a b
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement