K
K
knott2013-01-07 03:40:16
Functional programming
knott, 2013-01-07 03:40:16

Why is it customary in functional languages ​​to use the xs abbreviation for lists?

Sum of elements on OCaml (Wikipedia).

let rec sum xs =
  match xs with
    | []       -> 0
    | x :: xs' -> x + sum xs'


Same thing on scala (Stackoverflow).

def sum(xs: List[Int]): Int = {
  xs match {
    // if there is an element, add it to the sum of the tail
    case x :: tail => x + sum(tail) 

    // if there are no elements, then the sum is 0
    case Nil => 0 
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sl_bug, 2013-01-07
@knott

Most likely because "x" is an element and "s" is plural. A list is a set of elements. Something like item -> items

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question