Files
AILang/examples/std_list_more_demo.ailx
T
Brummel ee5807d73c Iter 15h — std_list extension: take, drop
Adds the two canonical prefix-slicing combinators to std_list.
First fns in std_list to combine `if` + Int arithmetic + recursive
ADT pattern in one body.

  take : forall a. (Int, List<a>) -> List<a>
       — first n elements (or all of xs if shorter than n).
  drop : forall a. (Int, List<a>) -> List<a>
       — list with first n elements removed (Nil if n >= length).

Both use `(if (<= n 0) ...)` as the base-case guard since
literal sub-patterns inside Ctor patterns aren't yet supported
(queued as 16c — would let take/drop collapse the if into the
match).

examples/std_list_more_demo: builds [1, 2, 3, 4, 5] once and
exercises take/drop at n=0, n=mid, n=overflow on each. Expected
output 0, 3, 5, 5, 3, 0 (one per line).

Tests: 95/95 (e2e went from 35 to 36). std_list grew from 10 to
12 combinators; total stdlib 5 modules / 29 combinators. No new
compiler bug surfaced.

Pre-existing std_list defs are byte-identical (verified via diff
on .ailx and round-trip on .ail.json), so the five downstream
importers (std_list_demo, std_list_stress, std_either_list,
std_either_list_demo, list_map_poly) need no regeneration.
2026-05-07 20:05:45 +02:00

33 lines
1.3 KiB
Plaintext

; Iter 15h — demo for the new std_list combinators `take` and `drop`.
; Uses a top-level `(const xs)` for the canonical [1, 2, 3, 4, 5] —
; pure ctor expression, so a const is fine (same idiom as
; std_list_demo). Six checks pin both n=0, 0<n<length, and n>length
; boundaries on each combinator.
(module std_list_more_demo
(import std_list)
(const xs
(doc "The canonical [1, 2, 3, 4, 5] used across all checks.")
(type (con std_list.List (con Int)))
(body
(term-ctor std_list.List Cons 1
(term-ctor std_list.List Cons 2
(term-ctor std_list.List Cons 3
(term-ctor std_list.List Cons 4
(term-ctor std_list.List Cons 5
(term-ctor std_list.List Nil))))))))
(fn main
(doc "Expected output (one per line): 0, 3, 5, 5, 3, 0.")
(type (fn-type (params) (ret (con Unit)) (effects IO)))
(params)
(body
(seq (do io/print_int (app std_list.length (app std_list.take 0 xs)))
(seq (do io/print_int (app std_list.length (app std_list.take 3 xs)))
(seq (do io/print_int (app std_list.length (app std_list.take 100 xs)))
(seq (do io/print_int (app std_list.length (app std_list.drop 0 xs)))
(seq (do io/print_int (app std_list.length (app std_list.drop 2 xs)))
(do io/print_int (app std_list.length (app std_list.drop 100 xs)))))))))))