I was couries how i could implement a linked List in Roc
you can treat List
as a linked list. But you mean by yourself?
LinkedList elem : [Cons elem (LinkedList elem), Nil]
thanks
Oh cool, Sam followed up :-). Yeah pretty much the same as OCaml, Haskell, Elm, etc....
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 }
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)
Your conslist is not well defined for roc.
Fix by roc-agent :robot: :
Screenshot_diff.png
Thank you! roc-agent is smart :geek:
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