Stream: beginners

Topic: Bug with Bool patterns?


view this post on Zulip Brian Teague (Nov 07 2024 at 01:11):

Is this a bug? This example works:

collatzConjecture = \number ->
    when (number % 2) is
        0 -> (number // 2)
        _ -> (3 * number + 1)

but when using Bool, I get the following error

collatzConjecture = \number ->
    when (Num.isEven number) is
        Bool.true -> (number // 2)
        _ -> (3 * number + 1)

This pattern is malformed
16│          Bool.true -> (number // 2)
             ^^^^^^^^^

Full code

module [steps]

steps : U64 -> Result U64 _
steps = \number ->
    when number is
        0 -> Err "Input must be greater than 0"
        _ -> Ok (step number 0)

step = \number, count ->
    when number is
        1 -> count
        _ -> number |> collatzConjecture |> step (count + 1)

collatzConjecture = \number ->
    when (Num.isEven number) is
        Bool.true -> (number // 2)
        _ -> (3 * number + 1)

view this post on Zulip Nathan Kramer (Nov 07 2024 at 01:13):

If I remember correctly, pattern matching with bools doesn’t work at the moment

view this post on Zulip Brian Teague (Nov 07 2024 at 01:16):

Hm, I would think a Bool type would just be a tag union of [ True | False ]

view this post on Zulip Richard Feldman (Nov 07 2024 at 01:21):

yeah, it used to be - we changed it based on #ideas > opaque bools although #ideas > custom types would change it so that True and False would both have the type Bool and could be used in pattern matching

view this post on Zulip Brian Teague (Nov 07 2024 at 01:29):

OMG, yes please to the custom types proposal!

view this post on Zulip Richard Feldman (Nov 07 2024 at 01:30):

:smiley: want to drop a comment in the #ideas > custom types thread?


Last updated: Jul 06 2025 at 12:14 UTC