From fe029573cfe49b40150ebe799ab123d3e9617546 Mon Sep 17 00:00:00 2001 From: Scott Richmond Date: Thu, 12 Jun 2025 16:55:17 -0400 Subject: [PATCH] add foldr --- prelude.ld | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/prelude.ld b/prelude.ld index 162fd2a..f123366 100644 --- a/prelude.ld +++ b/prelude.ld @@ -228,6 +228,22 @@ fn fold { } } + +fn foldr { + "Folds a list, right-associative." + (f as :fn, []) -> [] + (f as :fn, xs as :list) -> fold (f, xs, f ()) + (f as :fn, [], root) -> [] + (f as :fn, xs as :list, root) -> loop (root, first (xs), rest (xs)) with { + (prev, curr, []) -> f (curr, prev) + (prev, curr, remaining) -> recur ( + f (curr, prev) + first (remaining) + rest (remaining) + ) + } +} + & TODO: optimize these with base :conj! fn map { "Maps a function over a list: returns a new list with elements that are the result of applying the function to each element in the original list. E.g., `map ([1, 2, 3], inc) &=> [2, 3, 4]`." @@ -1359,6 +1375,7 @@ pkg Prelude { floor & math fn? & functions fold & lists + foldr & lists forward! & turtles get & dicts goto! & turtles