Skip to content

Instantly share code, notes, and snippets.

@christiaanb
Created April 23, 2015 15:18
Show Gist options
  • Save christiaanb/95e86618ffccd100aede to your computer and use it in GitHub Desktop.
Save christiaanb/95e86618ffccd100aede to your computer and use it in GitHub Desktop.
import Data.List
indices [] ix2 = ix2
indices ix1 [] = ix1
indices ((l1,r1):ix1) ((l2,r2):ix2)
| r1 < r2 = (l1,r1) : indices ix1 ((r1+1,r2):ix2)
| r1 == r2 = (l1,r1) : indices ix1 ix2
| r1 > r2 = (l2,r2) : indices ((r2+1,r1):ix1) ix2
length2indices :: [Int] -> [(Int,Int)]
length2indices lns = zip beginnings ends
where
edges = snd $ mapAccumL (\acc l -> let z = (l+acc) in (z,z)) 0 lns
beginnings = 0 : init edges
ends = map (subtract 1) edges
test = foldr1 indices $ map length2indices [[2,3,1,8],[2,2,7],[2,8]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment