Scala
Scala
2 Fonctions 2
2.1 Fonctions d’ordre supérieur . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2.2 Fonctions anonymes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2.3 Currification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2.4 Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
4 Lists 3
5 Tuples 3
5.1 Structural induction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
6 ’For’ syntax 3
7 Pattern matching 3
9 Constraints 4
10 Stream computation 4
11 Lisp 4
1
1 Syntax summary
– case sensitive
– Types: Int, Double, Byte, Short, Char, Long, Float, String, Boolean (True, False), Unit (commands),
Option
– Interaction with Java (java.lang.System.out.println)
– Operators: || && <==> ! = / ∗ % + − ˆ
– { . . . } = block
– if-then-else:
def abs(x:Double) = if (x >= 0) x else -x;
– for: for (s) yield e
1.1 Definitions
– def f = expr; // evaluated each time = definition
– def pi = 3.1415926;
– def square(x:Double)= x*x;
– def gcd(a:Int, b:Int):Int = if (b==0) a else gcd(b,a%b);
– def factorial(n:Int):Int = if (n==0)1 else n * factorial(n-1);
– val x = expr; // evaluated only one time = value !
– val y = sqrt(2);
2 Fonctions
2.1 Fonctions d’ordre supérieur
= les fonctions qui prennent d’autres fonctions en paramètre ou qui en retournent
2.3 Currification
def sum(f:(Int)Double) = {
def sumF(a:Int, b:Int):Double =
if (a > b) 0 else f(a) + sumF(a+1, b);
sumF
}
2.4 Polymorphism
Le type est passé en paramètre à la fonction:
def length[a](xs:List[a]):Int =
if(xs.isEmpty) Nil else ins(xs.head, isort(xs.tail));
2
3 Objets & classes
– presque comme en Java (import ... )
– auto-référence: this
– classes abstraites: abstract class ...with {...}
– héritage: class ...extends ...with {...}
– appel de méthode: objet.methode (opérateur infixé ’.’)
4 Lists
– Ex:
val eye3 = [[1,0,0],[0,1,0],[0,0,1]];
val sanch = ["toto", "tata"];
– the elements of a list must all have the same type
– empty list : Nil
– constructor: x::xs
– operators: head (1st element), tail (all elements except 1st one), isEmpty
– concatening operator: ’:::’
– combinators: map, filter, fold, foldRight, zip, flatMap, ...
– xs.map(x => x*x)
– xs.filter(x => x>0)
– def product(xs:List[Int])=(1::xs).reduce(x,y => x*y);
– def product(xs:List[Int])=xs.fold(x,y => x*y)(1);
5 Tuples
– elements can be of different types, fixed size
– ex: val xy = (2,3)
– selectors: 1, . . . , n. ex: val xy._1 + xy._2
– function zip :
def scalarProduct(xs:List[Double], ys:List[Double]):Double=(xs zip ys)
.map(xy => xy._1 * xy._2).fold(x:Double, y:Double => x+y)(0);
6 ’For’ syntax
– for (s) yield e
– persons.filter(p => p.age > 20).map(p => p.name) équivalent à
– for {val p <- persons; p.age > 20} yield p.name
7 Pattern matching
– reverse the normal construction process : subclass used + constructor argument → constructor
– e.match{case p1 => e1 ...case pn => en }
– ’ ’ is a wildcard pattern (case (_,[]) => [])
3
abstract class Expr with {
def derive(v:Var):Expr = this.match {
case Number(_) => Number(0)
case Var(name) => if (name == v.name) Number(1) else Number(0)
case Sum(e1,e2) => Sum(e1.derive(v),e2.derive(v))
case Prod(e1,e2) => Sum(Prod(e1,e2.derive(v)),Prod(e2,e1.derive(v)))
}
}
8.2 Loops
– while (condition){instructions}
– for(val i <- range(1,3)) do instructions
9 Constraints
ex: conversion ◦ C ↔ ◦ F (C*9 = (F-32)*5)
10 Stream computation
Streams are delayed lists ! Streams vs Lists :
Streams Lists
EmptyStream []
ConsStream x::xs
11 Lisp