Stream: ideas

Topic: ✔ function to split a List into head and tail


view this post on Zulip Richard Feldman (Nov 04 2023 at 20:26):

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.

view this post on Zulip Richard Feldman (Nov 04 2023 at 20:26):

I definitely do not want to introduce special pattern matching syntax for this like Elm and Haskell have.

view this post on Zulip Richard Feldman (Nov 04 2023 at 20:26):

anyone have thoughts on what a good name would be for this function?

view this post on Zulip Richard Feldman (Nov 04 2023 at 20:26):

for reference, here are the current List operations

view this post on Zulip Agus Zubiaga (Nov 04 2023 at 20:33):

It’s similar to String.uncons in Elm, but that’s a pretty bad name

view this post on Zulip Agus Zubiaga (Nov 04 2023 at 20:34):

Too bad List.splitFirst is already taken

view this post on Zulip Isaac Van Doren (Nov 04 2023 at 20:42):

What about List.splitHead or List.headAndTail?

view this post on Zulip Richard Feldman (Nov 04 2023 at 20:44):

popFirst and popLast are maybe interesting

view this post on Zulip Isaac Van Doren (Nov 04 2023 at 20:45):

I do like that those names work for the first and last elements

view this post on Zulip Brendan Hansknecht (Nov 04 2023 at 21:16):

Don't we already have pattern matching syntax for this?

view this post on Zulip Brendan Hansknecht (Nov 04 2023 at 21:16):

[head, .. as rest]

view this post on Zulip Richard Feldman (Nov 04 2023 at 21:16):

:face_with_raised_eyebrow: does that work?

view this post on Zulip Brendan Hansknecht (Nov 04 2023 at 21:16):

It should

view this post on Zulip Brendan Hansknecht (Nov 04 2023 at 21:17):

I know it had a bug at one point

view this post on Zulip Brendan Hansknecht (Nov 04 2023 at 21:17):

Not sure if it got fixed

view this post on Zulip Isaac Van Doren (Nov 04 2023 at 21:19):

There is this issue for reference https://github.com/roc-lang/roc/issues/5187

view this post on Zulip Richard Feldman (Nov 04 2023 at 21:23):

huh, cool! Okay, then fixing the bug seems like a good solution to this :big_smile:

view this post on Zulip Notification Bot (Nov 04 2023 at 21:23):

Richard Feldman has marked this topic as resolved.


Last updated: Jun 16 2026 at 16:19 UTC