Stream: beginners

Topic: Why is this pattern malformed?


view this post on Zulip Asbjørn Olling (Dec 04 2023 at 12:00):

Very minor AoC day 4 part 1 spoiler

The roc compiler gives me a syntax error , and tells me that "This pattern is malformed", and points at the two Bool.true things.
What am I doing wrong?

view this post on Zulip Asbjørn Olling (Dec 04 2023 at 12:02):

Maybe related, but I originally tried making the third pattern (_, Bool.false), but if I do that, Roc tells me that I'm missing the pattern (_, _). That seems weird? Surely the only possible values of Bool is Bool.true and Bool.false?

view this post on Zulip Asbjørn Olling (Dec 04 2023 at 12:03):

Maybe I'm just not allowed to pattern match on bools in general? Is that a design decision? I guess I could nest some if expressions instead?

view this post on Zulip Asbjørn Olling (Dec 04 2023 at 12:04):

Anyway, help and advice is much appreciated here. I really feel like there's something about Bools that I'm not understanding fully.

view this post on Zulip Anton (Dec 04 2023 at 12:40):

Asbjørn Olling said:

Maybe related, but I originally tried making the third pattern (_, Bool.false), but if I do that, Roc tells me that I'm missing the pattern (_, _). That seems weird? Surely the only possible values of Bool is Bool.true and Bool.false?

I think this is a known issue.

view this post on Zulip Asbjørn Olling (Dec 04 2023 at 12:44):

Coolcool. Good to know. That at least explains why I could have (_, Bool.false) in my third pattern.
I'm still confused by why the first two patterns give a syntax error, though?

view this post on Zulip Anton (Dec 04 2023 at 12:45):

I'll take a look

view this post on Zulip Anton (Dec 04 2023 at 12:48):

Maybe I'm just not allowed to pattern match on bools in general?

I indeed think this is the case. I'll check if we've discussed this before...

view this post on Zulip Anton (Dec 04 2023 at 12:57):

We've had several lengthy discussions about it, we're considering a solution now.

view this post on Zulip Anton (Dec 04 2023 at 12:59):

I would recommend going with an if for now

view this post on Zulip Asbjørn Olling (Dec 04 2023 at 13:00):

I aleady did ;)
Worked around it with an if, and that seems to work fine.

cardPoints : Card -> Nat
cardPoints = \card ->
    List.walk card.myNums 0 \points, myNum ->
        isWin = List.contains card.winNums myNum
        if isWin && points == 0 then
            1
        else if isWin then
            points * 2
        else
            points

view this post on Zulip Asbjørn Olling (Dec 04 2023 at 13:02):

and now I understand that I wasn't misunderstanding pattern match syntax - it's just bools that are a bit funky for now.

view this post on Zulip Asbjørn Olling (Dec 04 2023 at 13:03):

it still feels a bit weird to me that bools aren't just tags like everything else (even Result is). It seems like if I just mapped Bool.true and Bool.false to tags True and False, all the pattern matching stuff would work.

view this post on Zulip Asbjørn Olling (Dec 04 2023 at 13:04):

I assume there's a good reason for it, that I just don't undestand yet - given that the tags approach would be the most obvious (right?)
Something performance related maybe?

view this post on Zulip Asbjørn Olling (Dec 04 2023 at 13:06):

Oh I found the good stuff.
https://docs.google.com/document/d/1a51n7eIEbPjCWnGaL-pWbZBsRfi55GVQwIdPQTUu49Q/edit#heading=h.kikaz4s9uvzo

I'll go get smart now

view this post on Zulip Asbjørn Olling (Dec 04 2023 at 13:06):

Thanks for the help, @Anton !


Last updated: Jul 06 2025 at 12:14 UTC