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

Tree @master (Download .tar.gz)

first.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
-->

### `first` ###

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

`first` evaluates its first argument to obtain a non-negative integer,
considered to be a desired length, and its second argument to obtain a list.
It then evaluates to the prefix of the given list of the desired length.

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

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

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

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

    |   (first 0 (literal foo))
    = ()

'<<SPEC'

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