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

后端 未结 6 2103
悲&欢浪女
悲&欢浪女 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:35

    Try something like this:

        (define (slice l offset length)
          (if (null? l)
            l
            (if (> offset 0)
                (slice (cdr l) (- offset 1) length)
                (if (> length 0)
                    (cons (car l) (slice (cdr l) 0 (- length 1)))
                    '()))))
    

提交回复
热议问题