How to access an element in a list? #3965
-
hey, I am a newbie who starts learning gleam not soon. Recently I have been writing some exercises to create lists and write loops. I found it hard to do nested loops and access something in gleam. So a practical problem is how to access a specific position in a list. I searched many tutorials and examples online, but not getting any solution yet. So I wonder if there are any good examples that I can follow. import gleam/int
let r = int.random(100) // a random index
let a = list.repeat(0, 100) // the list
// how to do `a[r]` here, like python does |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
You don't want to do list access in gleam. Gleam's lists are linked lists, meaning it's |
Beta Was this translation helpful? Give feedback.
-
Yup, Gears is right! To add to that, you're going to have a hard time if you port Python algorithms to Gleam directly as one is mutable and object oriented, while the other is immutable and functional. You'll need to program in the functional style instead! |
Beta Was this translation helpful? Give feedback.
You don't want to do list access in gleam. Gleam's lists are linked lists, meaning it's
O(n)
to access an element. You should model your problem differently.