Tutorial by Examples

0.18.0 Prior to 0.18.0 you can create ranges like this: > range = [1..5] [1,2,3,4,5] : List number > > negative = [-5..3] [-5,-4,-3,-2,-1,0,1,2,3] : List number 0.18.0 In 0.18.0 The [1..5] syntax has been removed. > range = List.range 1 5 [1,2,3,4,5] : List number > &g...
> listOfNumbers = [1,4,99] [1,4,99] : List number > > listOfStrings = ["Hello","World"] ["Hello","World"] : List String > > emptyList = [] -- can be anything, we don't know yet [] : List a > Under the hood, List (linked list) is ...
> ourList = [1,2,3,4,5] [1,2,3,4,5] : List number > > firstElement = List.head ourList Just 1 : Maybe Int > > allButFirst = List.tail ourList Just [2,3,4,5] : Maybe (List Int) This wrapping into Maybe type happens because of the following scenario: What should List.head ret...
List.map : (a -> b) -> List a -> List b is a higher-order function that applies a one-parameter function to each element of a list, returning a new list with the modified values. import String ourList : List String ourList = ["wubba", "lubba", "dub",...
List.filter : (a -> Bool) -> List a -> List a is a higher-order function which takes a one-parameter function from any value to a boolean, and applies that function to every element of a given list, keeping only those elements for which the function returns True on. The function that List.f...
We can match on lists like any other data type, though they are somewhat unique, in that the constructor for building up lists is the infix function ::. (See the example Creating a list for more on how that works.) matchMyList : List SomeType -> SomeOtherType matchMyList myList = case myL...
List doesn't support "random access", which means it takes more work to get, say, the fifth element from the list than the first element, and as a result there's no List.get nth list function. One has to go all the way from the beginning (1 -> 2 -> 3 -> 4 -> 5). If you need ra...
In Elm, reducing functions are called "folds", and there are two standard methods to "fold" values up: from the left, foldl, and from the right, foldr. > List.foldl (+) 0 [1,2,3] 6 : number The arguments to foldl and foldr are: reducing function: newValue -> accumul...
> List.repeat 3 "abc" ["abc","abc","abc"] : List String You can give List.repeat any value: > List.repeat 2 {a = 1, b = (2,True)} [{a = 1, b = (2,True)}, {a = 1, b = (2,True)}] : List {a : Int, b : (Int, Bool)}
By default, List.sort sorts in ascending order. > List.sort [3,1,5] [1,3,5] : List number List.sort needs the list elements to be comparable. That means: String, Char, number (Int and Float), List of comparable or tuple of comparable. > List.sort [(5,"ddd"),(4,"zzz"),...
List.sortWith allows you to sort lists with data of any shape - you supply it with a comparison function. compareBools : Bool -> Bool -> Order compareBools a b = case (a,b) of (False, True) -> LT (True, False) -> GT _ -> ...
Note: this is not very efficient due to the nature of List (see Remarks below). It will be better to construct the list the "right" way from the beginning than to construct it and then reverse it. > List.reverse [1,3,5,7,9] [9,7,5,3,1] : List number
By default List.sort sorts in ascending order, with the compare function. There are two ways to sort in descending order: one efficient and one inefficient. The efficient way: List.sortWith and a descending comparison function. descending a b = case compare a b of LT -> GT ...
List.sortBy allows to use a function on the elements and use its result for the comparison. > List.sortBy String.length ["longest","short","medium"] ["short","medium","longest"] : List String -- because the lengths are: [7,5,6] It ...

Page 1 of 1