Stream: beginners

Topic: ✔ match list patterns


view this post on Zulip removewingman (Sep 04 2026 at 16:21):

This works:

b = |lst| {
    match lst {
        [h, ..] if h < 4 => "a"
        _ => ""
    }
}
expect [3] |> b |> Str.is_eq("a")
expect []  |> b |> Str.is_eq("")

But if I want to have tail as an variable it does not work:
Error: I was parsing a pattern, and this token cannot start a pattern here. [h, .. as _t] if h < 4 => "a"

b = |lst| {
    match lst {
        [h, .. as _tail] if h < 4 => "a"
        _ => ""
    }
}

expect [3] |> b |> Str.is_eq("a")
expect []  |> b |> Str.is_eq("")

this should work right, or is there any other way to have an if with head and tail as variable?

view this post on Zulip Richard Feldman (Sep 04 2026 at 16:38):

oh it's just that the syntax is [h, ..tail]

view this post on Zulip Richard Feldman (Sep 04 2026 at 16:39):

no as needed, although I do think that's an obvious thing to reach for, and we should give a nicer error message for that - would you mind opening a github issue for that @removewingman?

view this post on Zulip removewingman (Sep 04 2026 at 16:46):

Im confused, when was the change I am on: nightly-2026-09-02-d2609e2

And got this error message:

 ✗ old list rest pattern ─────────────────────────────────────────────────────────────────────────── /tmp/xdd.roc:4:13

I was parsing a list pattern, and this uses the old rest syntax.

[h, ..tail] if h < 4 => "a"
    ^^^^^^

List rest patterns now use .. as name. The name is optional, but if it is present it must come after as.

For example:
    [first, .. as rest]

(Will upgrade to latest and try again)

view this post on Zulip Anton (Sep 04 2026 at 17:00):

That as syntax is supported on latest nightly and latest main branch:
https://github.com/roc-lang/roc/blob/main/test/echo/all_syntax_test.roc

view this post on Zulip Richard Feldman (Sep 04 2026 at 17:03):

oh it used to be ..tail - maybe we changed it for the new compiler :sweat_smile:

view this post on Zulip removewingman (Sep 04 2026 at 17:09):

This example does not work tho:

b = |lst| {
    match lst {
        [h, .. as _tail] if h < 4 => "a"
        _ => ""
    }
}

expect [3] |> b |> Str.is_eq("a")
expect []  |> b |> Str.is_eq("")

It is probably a bug right? Opened this issue: https://github.com/roc-lang/roc/issues/11109

I actually like, the ..tail syntax a little bit more.

view this post on Zulip Notification Bot (Sep 04 2026 at 17:28):

removewingman has marked this topic as resolved.

view this post on Zulip Luke Boswell (Sep 04 2026 at 23:08):

See https://github.com/roc-lang/roc/issues/7091


Last updated: Sep 24 2026 at 15:59 UTC