Advertisement
Ilshidur

Opérations sur des listes en Scala (maximum et somme)

Feb 25th, 2014
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.52 KB | None | 0 0
  1. // Somme des éléments d'une liste
  2.  
  3. def sum(xs: List[Int]): Int = {
  4.    
  5.     xs.size match {
  6.         case 0 => 0
  7.         case 1 => xs.head
  8.         case _ => xs.head + sum(xs.tail)
  9.     }
  10.        
  11. }
  12.  
  13. // Element maximum d'une liste :
  14.  
  15. def max(xs: List[Int]): Int = {
  16.    
  17.     xs.size match {
  18.         case 0 => throw new java.util.NoSuchElementException("C'est tout vide !");
  19.         case 1 => xs.head
  20.         case _ =>
  21.         if ( xs.head > max(xs.tail) )
  22.             return xs.head
  23.         else
  24.             return max(xs.tail)        
  25.     }
  26.    
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement