Zulip Chat Archive

Stream: new members

Topic: Function that Replaces a List Element with another at an Idx


Colin ⚛️ (May 29 2025 at 16:46):

Is there a function that takes in an index and an element of the type represented in the List such that it replaces the element in the List at the index? It would look something like (n : ℕ) → (a : ?a) → List ?a → List ?a where n is the index and a is the object I want to put into the list.

For example, if 3 is the index given, 4:Nat is the object I want to replace the object in the list, and the list was [1,2,3,4], then the output would be [1,2,4,4].

Arthur Paulino (May 29 2025 at 16:50):

I think docs#List.modify does what you need

#eval [1, 2, 3, 4].modify (· + 10) 2 -- [1, 2, 13, 4]

Evan Gao (May 29 2025 at 16:50):

are you looking for List.set? If there are frequent modifications on the list then it's best to use an Array

Aaron Liu (May 29 2025 at 16:52):

@loogle Nat → List ?a → ?a → List ?a

loogle (May 29 2025 at 16:52):

:search: List.set, List.insertIdx, and 17 more


Last updated: Dec 20 2025 at 21:32 UTC