Answer the question
In order to leave comments, you need to log in
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'
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question