• Aucun résultat trouvé

J-O-Caml (1)

N/A
N/A
Protected

Academic year: 2022

Partager "J-O-Caml (1)"

Copied!
14
0
0

Texte intégral

(1)

J-O-Caml (1)

jean-jacques.levy@inria.fr

Qinghua, November 19

Plan of this class

writing programs in Ocaml

functional programming

use of polymorphic types

pattern-matching

tour in the libraries

(2)

Functional programming

Scheme, SML, Ocaml, Haskell are functional programming languages

they manipulate functions

and try to reduce memory states

Lisp Scheme

ML

Haskell Ocaml

Caml Camllight

Ocaml

Jocaml SML

Installing Ocaml

google Ocaml

caml.inria.fr/index.en.html

download the system (Linux, MacOS, Windows)

results in:

- ocaml (interactive toplevel) - ocamlc (compiler)

(3)

Phrases at toplevel

g =λx. 2∗x + 1 f(x) = 2∗x+ 1

f ≡g

Scopes of definitions

let, let in, let and, let and in, let rec, let rec in ...

fine control in definition scopes

letx =M inN letx =M ; ;

letx1=M1 andx2 =M2inN letx1 =M1andx2=M2; ; let recx =M in N let recx =M ; ;

(4)

Scopes of definitions

let, let in, let and, let and in, let rec, let rec in ...

fine control in definition scopes

letx =M inN letx =M ; ;

letx1=M1 andx2 =M2inN letx1 =M1andx2=M2; ; let recx =M in N let recx =M ; ;

Scopes of definitions

fine control in definition scopes

(5)

Scopes of definitions

fine control in definition scopes

Basic types

int (integers) 1, 2, 3, ...

float (real numbers) 2.3, 1.2, 0.

char (characters) ‘a’, ‘b’, ‘c’, ...

bool (booleans) true, false

unit (void) ()

Compound built-in types

string (strings) “nihao”, ...

list (lists of any type) [1; 2], 3 :: [4; 6]

array (arrays of any type) [| 1; 2; 3; 4 |]

(6)

No overloading in Ocaml

3 + 4 (on integers)

4.5 +. 3.0 (on real numbers)

3 + 2.4 (not allowed)

(float_of_int 3) +. 2.4 (legal expression)

this is to ease type inference

Operations on compound types

“nihao”.[3] (character at position)

List.hd [1; 2], List.tl [1; 2] (head and tail of list)

[| 1; 2; 3; 4 |].(3) (element at some index in array)

Small examples on arrays

(7)

Small examples on arrays

Easy exercices

isPalindromic s returns true if s is palindrome

reverse s returns mirror image of s

(Use s.[i] <- c store character c at (i + 1) position in s)

(8)

More exercices

sort a sorts array a in place

(Use a.(i) <- x store x at (i + 1) position in a)

transpose a transposes matrix a in place

(Use a.(i).(j) <- x store x at (i + 1), (j + 1) position in a)

(also Array.make_matrix h w v creates h x w matrix filled with value v)

Objective for next classes

a labeling algorithm for bitmap graphics

(9)

Combien d’objets dans une image?

Jean!Jacques L"vy

INRIA

(10)
(11)
(12)

pixels

800

1200 1Mpix

10

6

(pictures elements)

Problem and Algorithm

(13)

What is an object?

• set of similar adjacent pixels -

similar ?

• simplification

-

grayscale images (255 values)

-

0 = black, 255 = white

-

similar = adjacent with close values

• give a disctinct number to each object

• number of objects is max of previous numbers

Labeling

15 objects in this picture 1

1

2 3

4 5 6

7

8 9

10

11 12

13 14

(14)

Exercise for next class

find an algorithm for the labeling algorithm

Références

Documents relatifs

• Scheme, SML, Ocaml, Haskell are functional programming languages.. • they

• recursive polymorphic types (alternative definition for binary trees). dimanche 10

• recursive polymorphic types (alternative definition for binary trees). New

• module Graphics is a portable graphics library (needs graphics.cma to be compiled as first argument of the ocamlc command). • module names ( List ) start with

• mutable values can’t have real polymorphic types (see below). • they are not considered as real values only values have true

• an colorful array of great number of colors to pick in to show final objects with distinct colors [take the one given on next slide]. jeudi 3

- scan pixels left-to-right, top-to-bottom giving a new object id each time a new object is met, can be done on dual-core with half image per core.. 2)

Caml is the version of Robin Milner’s ML language developed at INRIA by Xavier Leroy et al.. Types in ML are polymorphic with a powerful type