Skip to content

Commit

Permalink
add List/sum and List/range built-ins
Browse files Browse the repository at this point in the history
  • Loading branch information
Sipher committed Aug 22, 2024
1 parent bd592ca commit ae16b30
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/fun/builtins.bend
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,24 @@ List/filter (List/Cons x xs) pred =
(List/filter xs pred)
}

## Recursively sums all elements in a list of natural numbers.
# If the list is empty, returns 0.
# List/sum (xs: List(n24)) -> n24
List/sum (List/Nil) = 0
List/sum (List/Cons x xs) = (+ x (List/sum xs))

# Generates a list of natural numbers starting from `start` and ending at `end` (inclusive).
# If the start value is greater than the end value, returns an empty list.
# List/range (start: n24, end: n24) -> (List n24)
List/range start end =
if (> start end) {
(List/Nil)
} else {
(List/Cons start (List/range (+ start 1) end))
}



# String/equals(s1: String, s2: String) -> u24
# Checks if two strings are equal.
String/equals (String/Nil) (String/Nil) = 1
Expand Down

0 comments on commit ae16b30

Please sign in to comment.