git @ Cat's Eye Technologies Robin / master stdlib / rest.robin
master

Tree @master (Download .tar.gz)

rest.robin @masterraw · history · blame

;'<<SPEC'

<!--
Copyright (c) 2012-2024, Chris Pressey, Cat's Eye Technologies.
This file is distributed under a 2-clause BSD license.  See LICENSES/ dir.
SPDX-License-Identifier: LicenseRef-BSD-2-Clause-X-Robin
-->

### `rest` ###

    -> Tests for functionality "Evaluate Robin Expression (with List)"

`rest` evaluates its first argument to obtain a non-negative integer,
considered to be a desired position, and its second argument to obtain a
list.  It then evaluates to the suffix of the given list starting at the
desired position.  The position 0 indicates the beginning of the list.

    |   (rest 0 (list 1 2 3 4 5))
    = (1 2 3 4 5)

    |   (rest 3 (list 1 2 3 4 5))
    = (4 5)

    |   (rest 5 (list 1 2 3 4 5))
    = ()

    |   (rest 6 (list 1 2 3 4 5))
    ? abort (expected-list ())

    |   (rest 1 (literal foo))
    ? abort (expected-list foo)

    |   (rest 0 (literal foo))
    = foo

'<<SPEC'

(define rest (fun (n li)
  (bind rest-r (fun (self n li)
    (if (equal? n 0)
      li
      (self self (subtract n 1) (tail li))))
    (rest-r rest-r n li))))