0% found this document useful (0 votes)
45 views4 pages

Scala

This document provides an overview summary of the Scala programming language syntax and features. It covers data types and operators, functions including higher-order functions, objects and classes, lists and tuples, pattern matching, and functions with state including variables and loops. It also briefly mentions streams, constraints, and the relationship between Scala and Lisp.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views4 pages

Scala

This document provides an overview summary of the Scala programming language syntax and features. It covers data types and operators, functions including higher-order functions, objects and classes, lists and tuples, pattern matching, and functions with state including variables and loops. It also briefly mentions streams, constraints, and the relationship between Scala and Lisp.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

EPFL-IN SCALA

Arnaud Zufferey 4 juin 2002

Table des matières


1 Syntax summary 2
1.1 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

2 Fonctions 2
2.1 Fonctions d’ordre supérieur . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2.2 Fonctions anonymes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2.3 Currification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2.4 Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

3 Objets & classes 3

4 Lists 3

5 Tuples 3
5.1 Structural induction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

6 ’For’ syntax 3

7 Pattern matching 3

8 Functions & state 4


8.1 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
8.2 Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

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

def sum(f:(Int)Double, a:Int, b:Int):Double = {


if (a > b) 0 else f(a) + sum(f, a+1, b)}
def cube(x:Int):Double = x*x*x;
def sumCubes(a:Int, b:Int): Double = sum(cubes, a, b);

2.2 Fonctions anonymes


= notation plus courte pour les fonctions internes. Ex. : x => x*x

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
}

syntaxe spéciale (currifiée):


def sum(f:(Int)Double)(a:Int, b:Int):Double =
if (a > b) 0 else f(a) + sum(f)(a+1, b);

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é ’.’)

class Rational(x:Int, y:Int) with {


private def gcd(..):Int .. ;
def numer = x;
def denom = y;
override def toString() = ... }
val x1 = Rational(1,2); // ou new Rational..
val a = x1.numer;

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);

5.1 Structural induction

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 Functions & state


8.1 Variables
– var x:String = "abcdef"; x = "hello"
– var count = 111; count = count + 1;

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)

val C = new Quantity;


val F = new Quantity;
C*c(9) === (F + c(-32))*c(5);

10 Stream computation
Streams are delayed lists ! Streams vs Lists :
Streams Lists
EmptyStream []
ConsStream x::xs

11 Lisp

You might also like