Lists and LibrariesEdit on GitHub
Arguably the most important data structure in Grain, lists allow us work with collections of data.
Lists always contain elements of the same type, and attempting to mix element types will result in a compilation error.
Lists in Grain are linked lists, so if we’d like to add a new item to a list, we add it to the front:
We can also write functions that process data in lists, but we’ll save that fun for the section on Pattern Matching.
Lists wouldn’t be all that interesting if we couldn’t do anything with them, right? Conveniently, the Grain standard library provides a set of functions that work with lists.
We can import and use the List library like so:
The first line imports the whole list module, and we can access all values contained in the module. If desired, we could instead import only the values we intend to use:
To learn more about what’s available in the List standard library, check out the lists standard library documentation.