I'd like to add a builtin List function with this type:
List elem -> Result (elem, List elem) [ListWasEmpty]
so it gives you back the first element in the list followed by the rest of the list after it (as a seamless slice).
I specifically want this for parsers - it's a nice way to use a single conditional to advance to the next element in the list - although it may have other use cases.
I definitely do not want to introduce special pattern matching syntax for this like Elm and Haskell have.
anyone have thoughts on what a good name would be for this function?
for reference, here are the current List operations
It’s similar to String.uncons in Elm, but that’s a pretty bad name
Too bad List.splitFirst is already taken
What about List.splitHead or List.headAndTail?
popFirst and popLast are maybe interesting
I do like that those names work for the first and last elements
Don't we already have pattern matching syntax for this?
[head, .. as rest]
:face_with_raised_eyebrow: does that work?
It should
I know it had a bug at one point
Not sure if it got fixed
There is this issue for reference https://github.com/roc-lang/roc/issues/5187
huh, cool! Okay, then fixing the bug seems like a good solution to this :big_smile:
Richard Feldman has marked this topic as resolved.
Last updated: Jun 16 2026 at 16:19 UTC