Zulip Chat Archive
Stream: Is there code for X?
Topic: extract sublist from list
Johan Commelin (Sep 29 2021 at 06:59):
Does this exist already?
/-- Returns the sublist of `L` starting at index `m` of length `n`
(or shorter, if `L` is too short). -/
def list.extract {α : Sort*} (L : list α) (m n : ℕ) := ((L.split_at m).2.split_at n).1
#eval [0,1,2,3,4,5,6,7,8,9].extract 4 3 -- [4,5,6]
docs#list.slice will take a list L
, and two nats n
and m
, and return the list constructed from L
by dropping m
elements starting from index n
.
I'm interested in the part that is dropped. Is there already a function for that?
Johan Commelin (Sep 29 2021 at 06:59):
Is there a common name for this in list parlance in other languages?
Scott Morrison (Sep 29 2021 at 07:10):
I guess people usually just use list.take
and list.drop
.
Scott Morrison (Sep 29 2021 at 07:11):
#eval ([0,1,2,3,4,5,6,7,8,9].drop 4).take 3
Kevin Buzzard (Sep 29 2021 at 08:17):
This is some fundamental construction L[n,n+m]
in python, right? What's it called in Haskell?
Anne Baanen (Sep 29 2021 at 08:52):
AFAICT, "slicing" is the near-universal word for Johan's operation, so docs#list.slice does exactly the opposite from almost everyone else.
Last updated: Dec 20 2023 at 11:08 UTC