Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add List/sum and List/range built-ins #696

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Sipher marked this conversation as resolved.
Show resolved Hide resolved
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)
Sipher marked this conversation as resolved.
Show resolved Hide resolved
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
Loading