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

后端 未结 12 1027
南方客
南方客 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:50

    Why not use already existing Data.Vector.slice together with Data.Vector.fromList and Data.Vector.toList (see https://stackoverflow.com/a/8530351/9443841)

    import Data.Vector ( fromList, slice, toList )
    import Data.Function ( (&) )
    
    vSlice :: Int -> Int -> [a] -> [a]
    vSlice start len xs =
        xs
            & fromList 
            & slice start len
            & toList 
    

提交回复
热议问题