List is immutable, the underlying implementation uses the data structure on the linked list. Head is the first element, tail is the remaining element
Last is the final element, and Init is an element other than the last element
2. Inserting sorting algorithms
Package Chapter16object Testlist extends app{def isort (X:list[int]): list[int] = if (x.isempty) x else Insert (x. Head, Isort (x.tail)) def insert (X:int, Xs:list[int]): list[int] = {if (Xs.isempty | | x <= xs.head) x:: XS else Xs.head:: Insert (x, Xs.tail)} val list0 = List (4,5,3,6,1,7,0) println (Isort (list0))}
3. Using the pattern matching to improve the code
Package Chapter16object Testlist extends app{def isort (Xs:list[int]): list[int] = xs Match {case List () = xs Case X::XS1 = insert (x, Isort (XS1))} def insert (X:int, Xs:list[int]): list[int] = xs Match {case List () = = X::xs Case Y::ys = if (x <= y) x:: xs Else Y::insert (x, Ys)} val list0 = List (4,5,3,6,1,7,0) PR Intln (Isort (list0))}
4. First-order method
This section explains most first-order methods defined in theList class. A method is first-order If it does not take any functions as arguments.
A method that does not take a function as a parameter is called a first-order method
5. Stitching to implement reverse method
DEF reverse (Xs:list[int]): list[int] = xs Match {case List () + xs case y::ys = = Reverse (ys)::: List (y)} V Al List0 = List (4,5,3,6,1,7,0) println (reverse (list0))//list (0, 7, 1, 6, 3, 5, 4)
6. Flat List
The flatten method takes a list of lists and flattens it out to a single list
7.drop Remove the top N, take the first n
8.map corresponds to lapply in the R language
9.flatmap if F returns a list, all the return values are stitched together to return
Programming in Scala (Second Edition) Reading notes 15 using list