Stream: beginners

Topic: How couuld i implemend smth like a linked List in Roc


view this post on Zulip Jonas (Dec 05 2024 at 21:39):

I was couries how i could implement a linked List in Roc

view this post on Zulip Anthony Bullard (Dec 05 2024 at 21:40):

you can treat List as a linked list. But you mean by yourself?

view this post on Zulip Sam Mohr (Dec 05 2024 at 21:44):

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

view this post on Zulip Jonas (Dec 05 2024 at 21:45):

thanks

view this post on Zulip Anthony Bullard (Dec 05 2024 at 21:46):

Oh cool, Sam followed up :-). Yeah pretty much the same as OCaml, Haskell, Elm, etc....

view this post on Zulip Tom Hill (May 05 2025 at 21:09):

I'm implementing this as part of one of the exercism exercises and wondering why this won't compile:

SimpleLinkedList : {
    head : [Cons, Nil],
}

Cons : {
    value : U64,
    next : [Cons, Nil],
}

from_list : List U64 -> SimpleLinkedList
from_list = |list|
    node =
        list
        |> List.walk(
            Nil,
            |acc, value|
                { value, next: acc },
        )
    { head: node }

view this post on Zulip Fábio Beirão (May 05 2025 at 21:28):

I'm not sure, maybe due to the |acc, value|. What if you call it |acc, curr| and then do { value: curr, next: acc }
Also, why is there a trailing comma on { value, next: acc}, :thinking: (just brainfarting here, maybe none of these are the issue)

view this post on Zulip Brendan Hansknecht (May 05 2025 at 23:10):

Your conslist is not well defined for roc.

view this post on Zulip Anton (May 06 2025 at 10:12):

Fix by roc-agent :robot: :
Screenshot_diff.png

view this post on Zulip Tom Hill (May 06 2025 at 21:14):

Thank you! roc-agent is smart :geek:

view this post on Zulip Brendan Hansknecht (May 07 2025 at 01:52):

Thanks @Anton for following up. I meant to follow up when I got to a computer, but totally forgot.


Last updated: Jul 06 2025 at 12:14 UTC