Does Haskell have List Slices (i.e. Python)?

后端 未结 12 1034
南方客
南方客 2020-12-05 02:15

Does Haskell have similar syntactic sugar to Python List Slices?

For instance in Python:

x = [\'a\',\'b\',\'c\',\'d\']
x[1:3] 

give

12条回答
  •  不思量自难忘°
    2020-12-05 02:44

    Obviously my foldl version loses against the take-drop approach, but maybe someone sees a way to improve it?

    slice from to = reverse.snd.foldl build ((from, to + 1), []) where
       build res@((_, 0), _) _ = res  
       build ((0, to), xs) x = ((0, to - 1), x:xs)  
       build ((from, to), xs) _ = ((from - 1, to - 1), xs)
    

提交回复
热议问题