Files
.github
api-server
client
config
curriculum
docs
guide
arabic
chinese
english
3d
accessibility
agile
algorithms
android-development
angular
apache
aspnet
bash
blender
blockchain
bootstrap
bsd-os
bulma
c
canvas
certifications
chef
clojure
cloud-development
codeigniter
computational-genomics
computer-hardware
computer-science
containers
cplusplus
csharp
css
d3
data-science-tools
design-patterns
designer-tools
developer-ethics
developer-tools
devops
dns
docker
documentation
drupal
electron
elixir
elm
architecture
arrays
index.md
date
list
process
random
strings
task
tuples
index.md
erlang
fsharp
game-development
gatsbyjs
git
go
graphql
groovy
haskell
haxe
hibernate
html
ionic
java
javascript
joomla
jquery
julia
kotlin
laravel
linux
logic
machine-learning
mariadb
mathematics
meta
miscellaneous
mobile-app-development
mongodb
natural-language-processing
neovim
network-engineering
nginx
nodejs
optical-alignment
php
powershell
product-design
progressive-web-apps
puppet
python
r
raspberry-pi
react
react-native
redux
rest-api
robotics
rt-os
ruby
rust
sass
security
semantic-ui
software-engineering
sql
ssh
svg
svn
swift
terminal-commandline
tomcat
tools
typescript
typography
user-experience-design
user-experience-research
vagrant
vim
virtualbox
visual-design
voice
vue
vue-cli
web-augmented-reality
web-components
web-performance
web-virtual-reality
wordpress
working-in-tech
xml
portuguese
russian
spanish
mock-guide
tools
.editorconfig
.eslintignore
.eslintrc.json
.gitattributes
.gitignore
.node-inspectorrc
.prettierrc
.snyk
.travis.yml
.vcmrc
CODE_OF_CONDUCT.md
CONTRIBUTING.md
LICENSE.md
README.french.md
README.italian.md
README.md
docker-compose-shared.yml
docker-compose.yml
lerna.json
netlify.toml
package-lock.json
package.json
sample.env
freeCodeCamp/guide/english/elm/arrays/index.md

40 lines
1.9 KiB
Markdown
Raw Normal View History

---
title: Arrays
---
## Arrays
Elm does not offer arrays in the strict sense of the word, having lists instead. This is an important distinction, as lists and arrays offer different guarantees regarding the time complexity of their operations. The full API for Elm lists can be found in the [official documentation](https://package.elm-lang.org/packages/elm/core/latest/List).
### Syntax
List literals can be declared in a similar fashion to many other languages:
```elm
[1, 2, 3]
```
The language also offers other utility functions to build lists, like [`singleton`](https://package.elm-lang.org/packages/elm/core/latest/List#singleton), [`repeat`](https://package.elm-lang.org/packages/elm/core/latest/List#repeat), [`range`](https://package.elm-lang.org/packages/elm/core/latest/List#range) and the [cons operator](https://package.elm-lang.org/packages/elm/core/latest/List#::):
```elm
singleton 1 == [1]
repeat 3 0 == [0, 0, 0]
range 0 2 == [0, 1, 2]
1 :: 2 :: 3 :: [] == [1, 2, 3]
```
### Operations
Like expected from a functional language, Elm offers functions like `map`, `filter` and `reduce` (called `foldl` and `foldr` in Elm) for transforming lists. The only difference from JavaScript here is the reduce functions:
- `foldl` applies the reducing function from left to right (i.e. starting from the first element of the list)
- `foldr` applies the reducing function from right to left (i.e. starting from the last element of the list)
Here is an example below (You can try this in the elm repl to understand it better in practice):
```elm
List.foldl (::) [] [1, 2, 3] == [3, 2, 1] -- [] |> (::) 1 |> (::) 2 |> (::) 3
List.foldr (::) [] [1, 2, 3] == [1, 2, 3] -- [] |> (::) 3 |> (::) 2 |> (::) 1
```
The core List library also offers several other functions for sorting, combining and deconstructing lists. A more detailed description of those can be found in the [official documentation](https://package.elm-lang.org/packages/elm/core/latest/List).