fork download
  1. -- 把一个字符串中1存在的位置区间表现出来,例如“00101111110111101110 => 3, 5-10, 12-15, 17-19”
  2. -- mark digit 1 position in a binary string
  3.  
  4. import Data.List
  5. digit1Length str = filter (\e -> (fst e)>0) . tail . scanl (\z e -> (if '1'==head e then snd z + 1 else 0, snd z + (length e)) ) (0,0) $ group str
  6.  
  7. main = do
  8. print $ digit1Length "00101111110111101110"
  9. print $ map (\e -> if fst e == snd e then show $ fst e else show (fst e) ++ "-" ++ show (snd e)) $ digit1Length "00101111110111101110"
  10.  
  11.  
Success #stdin #stdout 0s 4652KB
stdin
Standard input is empty
stdout
[(3,3),(5,10),(12,15),(17,19)]
["3","5-10","12-15","17-19"]