List Operation: Prepend and Append
Prepending and appending an element to a list:
1 :: List(2, 3) // List(1, 2, 3)
1 +: List(2, 3) // List(1, 2, 3)
List(2, 3) :+ 1
// not working: List(2, 3) :: 1List Operation: Map
Passing anonymous functions (lambda expressions) to a higher-order function map, increasingly omitting details such as argument types and names:
List(11,21,32).map((x:Int) => x+1)
List(11,21,32).map(x => x+1)
List(11,21,31).map(_ + 1) // List(12,22,32)List Operation: Filter
Variations of the filter implementation Functions on Lists: Slide 23:
If in body of case
def filter [X] (xs:List[X], f:X=>Boolean) : List[X] = xs match
case Nil => Nil
case y::ys =>
if f(y) then y :: filter (ys, f)
else filter (ys, f)Explicit negated condition
def filter [X] (xs:List[X], f:X=>Boolean) : List[X] = xs match
case Nil => Nil
case y::ys if f (y) => y :: filter (ys, f)
case _::ys if !f (y)=> filter (ys, f)