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 equals and tail to the list.bend example #667

Merged
merged 1 commit into from
Aug 8, 2024
Merged
Changes from all commits
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
29 changes: 29 additions & 0 deletions examples/list.bend
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,35 @@ head = @l
List/Nil: []
}

# List tail:
# List l -> List
# returns the list except for the first item in it, or [] if the list is empty.
def tail(l):
match l:
case List/Cons:
return l.tail
case List/Nil:
return []

# List equals:
# List xs, List ys, function cmp -> 1|0
# Compares the elements in two lists and returns 1 if they're equal, and 0 otherwise.
# The function cmp compares two values and returns 1 if they're equal, and 0 otherwise.
equals xs ys cmp = match xs {
List/Cons: match ys {
List/Cons: if (cmp xs.head ys.head) {
(equals xs.tail ys.tail cmp)
} else {
0
}
List/Nil: 0
}
List/Nil: match ys {
List/Cons: 0
List/Nil: 1
}
}

# List pop_front:
# List l -> List l
# removes and discards the first item of list l.
Expand Down
Loading