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

Tree @master (Download .tar.gz)

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

### `length` ###

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

`length` evaluates its single argument to obtain a proper list, then
evaluates to a non-negative integer which is the length of the list
(the number of cells, not counting nested cells and not counting the
empty list at the very tail.)

    |   (length ())
    = 0

    |   (length (list 1 2 #t #f 3))
    = 5

    |   (length (literal whatnot))
    ? abort (expected-list whatnot)

'<<SPEC'

(define length (fun (li)
  (subtract 0 (fold (fun (x acc) (subtract acc 1)) 0 li))))