How do I take a slice of a list (A sublist) in scheme?

后端 未结 6 2082
悲&欢浪女
悲&欢浪女 2020-12-31 07:09

Given a list, how would I select a new list, containing a slice of the original list (Given offset and number of elements) ?

EDIT:

Good suggestions so far. I

6条回答
  •  执念已碎
    2020-12-31 07:34

    Strangely, slice is not provided with SRFI-1 but you can make it shorter by using SRFI-1's take and drop:

    (define (slice l offset n)
      (take (drop l offset) n))
    

    I thought that one of the extensions I've used with Scheme, like the PLT Scheme library or Swindle, would have this built-in, but it doesn't seem to be the case. It's not even defined in the new R6RS libraries.

提交回复
热议问题