Stream: beginners

Topic: ✔ walkWithIndex needs its 3rd argument to be


view this post on Zulip Jason Hobbs (Dec 03 2023 at 18:31):

I've been struggling to understand what' s wrong here. I get that Int Natural isn't a Nat, but don't understand why. Any insights would be appreciated.

Also, with these kind of errors, I've had some success by giving named functions signatures like x : I16 to communicate intent. Then, the compiler gives me a clearer answer. I'm not sure how to do that with anonymous functions though.

── TYPE MISMATCH ──────────────────────────────────────────────────── day3.roc ─

This 3rd argument to walkWithIndex has an unexpected type:

120│>    List.walkWithIndex identifiedParts [] \state, cell, index ->
121│>      when cell.cell is
122│>        Part num ->
123│>          adjacentSymbols =
124│>            List.map adjacentCellIndexes \relativeIndex -> (Num.add relativeIndex (Num.toI16 index))
125│>            |> List.keepIf \absoluteIndex -> absoluteIndex >= 0
126│>            |> List.map Num.toNat
127│>            |> List.keepOks \absoluteIndex -> List.get identifiedParts absoluteIndex
128│>            |> List.keepIf \adjacentCell ->
129│>                  when adjacentCell.cell is
130│>                    Symbol -> Bool.true
131│>                    _ -> Bool.false
132│>
133│>          List.append state {
134│>            cell &
135│>            type: if List.isEmpty adjacentSymbols then Extra else Correct
136│>          }
137│>
138│>        _ -> List.append state {
139│>                cell &
140│>                type: NotAPart
141│>              }

The argument is an anonymous function of type:

    List { … }d, {
        cell : […]*,
        type : [
            Correct,
            Extra,
            NotAPart,
        ]*,
        …
    }d, Int Natural -> List { … }d

But walkWithIndex needs its 3rd argument to be:

    List { … }d, { cell : […], … }, Nat -> List { … }d

view this post on Zulip Brian Carroll (Dec 03 2023 at 18:45):

You can't annotate anonymous functions unfortunately. If I had this problem I'd probably give it a name (either as a local variable or at top level). You could make it anonymous again after debugging it. But if it needed debugging I usually take that as a sign that it really needs the signature.

view this post on Zulip Jason Hobbs (Dec 03 2023 at 18:49):

Is there a way to alias types? I haven't been able to figure that out yet either.

Color = Red | Green | Blue
State = { some: [ Big, TypeDefinition ] }

view this post on Zulip Elias Mulhall (Dec 03 2023 at 18:49):

I think the Nat thing is a red herring. The error is saying that the type for the anonymous function is not consistent

view this post on Zulip Elias Mulhall (Dec 03 2023 at 18:50):

Color : [Red] etc

view this post on Zulip Elias Mulhall (Dec 03 2023 at 18:52):

The clue that's a bit hard to see is the polymorphic record variable d.
List { ... }d, { cell : [...], ... }

view this post on Zulip Jason Hobbs (Dec 03 2023 at 18:53):

Elias Mulhall said:

The clue that's a bit hard to see is the polymorphic record variable d.
List { ... }d, { cell : [...], ... }

Yeah, that's not even a clue to me, yet. Does that hint at anything specific to you?

view this post on Zulip Richard Feldman (Dec 03 2023 at 18:55):

Jason Hobbs said:

Is there a way to alias types? I haven't been able to figure that out yet either.

Color = Red | Green | Blue
State = { some: [ Big, TypeDefinition ] }

you're almost there! You just need : instead of = for these

view this post on Zulip Richard Feldman (Dec 03 2023 at 18:56):

(if the syntax for type aliases was Color = ..., it would be ambiguous with a pattern match on a tag)

view this post on Zulip Jason Hobbs (Dec 03 2023 at 18:57):

Still troubleshooting here, but I think I found my mistake... The docs say "Note that & can't introduce new fields to a record", which is what I was trying to do

view this post on Zulip Elias Mulhall (Dec 03 2023 at 18:58):

Sorry, I was on my phone, switched to a real keyboard :laughing:

Yes!

    List { … }d, {
        cell : […]*,
        type : [
            Correct,
            Extra,
            NotAPart,
        ]*,
        …
    }d, Int Natural -> List { … }d

has a d at the end of each record, while

List { … }d, { cell : […], … }, Nat -> List { … }d

Is missing the d.

This indicates that the record types are not all the same.

view this post on Zulip Elias Mulhall (Dec 03 2023 at 19:00):

I'd start with Brian's advice and factor out a walker helper function to give it a type, that should make the error more friendly.

view this post on Zulip Jason Hobbs (Dec 03 2023 at 19:05):

The type aliases have made this much better! Also, the trick of moving the anonymous function up and naming it (to provide it type hints) was excellent. All of that got me unstuck. I'm compiling again!

And yes, @Elias Mulhall , the record types were the problem. I was trying to add an element with the & operator

view this post on Zulip Notification Bot (Dec 03 2023 at 19:06):

Jason Hobbs has marked this topic as resolved.


Last updated: Jul 06 2025 at 12:14 UTC