Stream: beginners

Topic: recursive data types ?


view this post on Zulip dank (Feb 13 2023 at 09:37):

like in zig it'd be done with a pointer like so

const Node = struct {
  next: ?*Node,
  data: i32,
};

view this post on Zulip Folkert de Vries (Feb 13 2023 at 11:12):

in roc syntactically you can just define recursive types

view this post on Zulip Folkert de Vries (Feb 13 2023 at 11:13):

in zig you couldn't write next: Node because that would be a type of infinite size

view this post on Zulip Folkert de Vries (Feb 13 2023 at 11:13):

but in roc there is implicitly a pointer around any recursive use of a type

view this post on Zulip Folkert de Vries (Feb 13 2023 at 11:14):

so the above would be equivalent to

LinkedList a : [ Nil, Cons a (LinkedList a) ]

view this post on Zulip dank (Feb 13 2023 at 11:35):

ahh thanks. I was looking for a doc on it, thought it wasn't implemented yet.

I do see now it's on here https://github.com/roc-lang/roc/blob/main/roc-for-elm-programmers.md#custom-types
pretty cool tnx

view this post on Zulip dank (Feb 13 2023 at 12:34):

Folkert de Vries said:

so the above would be equivalent to

LinkedList a : [ Nil, Cons a (LinkedList a) ]

one question folkert if you dont mind. why do we need the second a after Cons?
isn't it enough that we specify it after the LinkedList?
[it does seem to work without it but i might be missing something]

view this post on Zulip Folkert de Vries (Feb 13 2023 at 12:37):

here, Cons has two fields, one of type a (the data) and one of type LinkedList a (the next node)

view this post on Zulip dank (Feb 13 2023 at 12:39):

yes I understand. is it just for declarative purposes?
meaning could we have managed without it?

view this post on Zulip dank (Feb 13 2023 at 12:40):

oh nvm

view this post on Zulip dank (Feb 13 2023 at 12:41):

it's more like trying to resemble the structure of the outer type

view this post on Zulip Folkert de Vries (Feb 13 2023 at 12:41):

without that a there is not actual data in your linked list

view this post on Zulip Folkert de Vries (Feb 13 2023 at 12:41):

it's jus the spine (basically, you've encoded peano numbers at that point)

view this post on Zulip dank (Feb 13 2023 at 12:42):

haha yes

view this post on Zulip dank (Feb 13 2023 at 12:42):

that makes sense. i read it wrong initially


Last updated: Jul 05 2025 at 12:14 UTC