# letfill_array
tree
tab
empty
=
let
rec
aux
i
p
=
function
Empty
->
tab
.
(i)
<-
empty
|
Node
(l
,
e,
r)->
tab
.
(i)
<-
e
;
aux
(i
+
1
)(p
/
2
)l
;
aux
(i
+
p)(p
/
2
)r
in
aux
0
(((Array.length
tab)
+
1
)/
2
)tree
;;
val fill_array : 'a arbre_bin -> 'a array -> 'a -> unit = <fun>
# type'a
arbre_bin
=
Empty
|
Node
of
'a
arbre_bin
*
'a
*
'a
arbre_bin
;;
type 'a arbre_bin = | Empty | Node of 'a arbre_bin * 'a * 'a arbre_bin
# letleaf
empty
=
[|
empty
|]
;;
val leaf : 'a -> 'a array = <fun>
# letnode
elt
left
right
=
let
ll
=
Array.length
left
and
lr
=
Array.length
right
in
let
l
=
max
ll
lr
in
let
res
=
Array.create
(
2
*
l+
1
)elt
in
Array.blit
left
0
res
1
ll
;
Array.blit
right
0
res
(ll
+
1
)lr
;
res
;;
val node : 'a -> 'a array -> 'a array -> 'a array = <fun>
# letrec
make_array
empty
=
function
Empty
->
leaf
empty
|
Node
(l
,
e,
r)->
node
e
(make_array
empty
l)
(make_array
empty
r)
;;
val make_array : 'a -> 'a arbre_bin -> 'a array = <fun>
# letinfixe
tab
empty
f
=
let
rec
aux
i
p
=
if
tab
.
(i)<>
emptythen
(
aux
(i
+
1
)(p
/
2
);
f
tab
.
(i);
aux
(i
+
p)(p
/
2
))
in
aux
0
(((Array.length
tab)
+
1
)/
2
);;
val infixe : 'a array -> 'a -> ('a -> 'b) -> unit = <fun>
# letprint_tab_int
tab
empty
=
infixe
tab
empty
(fun
x
->
print_int
x
;
print_string
" - "
);;
val print_tab_int : int array -> int -> unit = <fun>
# letprefixe
tab
empty
f
=
for
i
=
0
to
(Array.length
tab)
-
1
do
if
tab
.
(i)<>
emptythen
f
tab
.
(i)done
;;
val prefixe : 'a array -> 'a -> ('a -> unit) -> unit = <fun>
# typenoeud_lex
=
Lettre
of
char
*
bool
*
arbre_lex
and
arbre_lex
=
noeud_lex
list;;
type noeud_lex = | Lettre of char * bool * arbre_lex
type arbre_lex = noeud_lex list
# typemot
=
string;;
type mot = string
# letrec
existe
m
d
=
let
aux
sm
i
n
=
match
d
with
[]
->
false
|
(Lettre
(c
,
b,
l))::q->
if
c
=
sm
.[
i]
then
if
n
=
1
then
b
else
existe
(String.sub
sm
(i
+
1
)(n
-
1
))l
else
existe
sm
q
in
aux
m
0
(String.length
m);;
val existe : string -> arbre_lex -> bool = <fun>
# letrec
ajoute
m
d
=
let
aux
sm
i
n
=
if
n
=
0
then
d
else
match
d
with
[]
->
[
Lettre(sm
.[
i],
n
=
1
,
ajoute
(String.sub
sm
(i
+
1
)(n
-
1
))[])
]
|
(Lettre(c
,
b,
l))::q->
if
c
=
sm
.[
i]
then
if
n
=
1
then
(Lettre(c
,
true,
l))::q
else
Lettre(c
,
b,
ajoute(String.sub
sm
(i
+
1
)(n
-
1
))l)::q
else
(Lettre(c
,
b,
l))::(ajoutesm
q)
in
aux
m
0
(String.length
m);;
val ajoute : string -> arbre_lex -> arbre_lex = <fun>
# letrec
verifie
l
d
=
match
l
with
[]
->
[]
|
t::q
->
if
existe
t
d
then
t::(verifie
q
d)
else
verifie
q
d
;;
val verifie : string list -> arbre_lex -> string list = <fun>
# letstring_of_char
c
=
String.make
1
c;;
val string_of_char : char -> string = <fun>
# letrec
filter
p
l
=
match
l
with
[]
->
[]
|
t::q
->
if
p
t
then
t::(filter
p
q)
else
filter
p
q;;
val filter : ('a -> bool) -> 'a list -> 'a list = <fun>
# letrec
selecte
n
d
=
match
d
with
[]
->
[]
|
(Lettre(c
,
b,
l))::q->
if
n
=
1
then
filter
(function
x
->
x
<>
"!"
)
(List.map
(function
(Lettre(c
,
b,_
))->
if
b
then
string_of_char
c
else
"!"
)d)
else
let
r
=
selecte
(n
-
1
)l
and
r2
=
selecte
n
q
in
let
pr
=
List.map
(function
s
->
(string_of_char
c)
^
s)r
in
pr
@
r2;;val selecte : int -> arbre_lex -> string list = <fun>
# letlire_fichier
nom_fichier
=
let
dico
=
ref
[]
and
canal
=
open_in
nom_fichier
in
try
while
true
do
dico
:=
ajoute
(input_line
canal)
!
dicodone
;
failwith
"cas impossible"
with
End_of_file
->
close_in
canal
;
!
dico
|
x
->
close_in
canal
;
raise
x
;;
val lire_fichier : string -> arbre_lex = <fun>
# letmots
s
=
let
est_sep
=
function
' '
|
'\t'
|
'\''
|
'"'
->
true
|
_
->
false
in
let
res
=
ref
[]
and
p
=
ref
((String.length
s)
-
1
)in
let
n
=
ref
!
pin
while
!
p>=
0
&&
est_sep
s
.[!
p]
do
decr
p
done
;
n
:=
!
p;
while
(
!
n>=
0
)do
while
!
n>=
0
&&
not
(est_sep
s
.[!
n]
)do
decr
n
done
;
res
:=
String.sub
s
(
!
n
+
1
)(
!
p
-
!
n)::
!
res;
while
!
n>=
0
&&
est_sep
s
.[!
n]
do
decr
n
done
;
p
:=
!
n
done
;
!
res;;
val mots : string -> string list = <fun>
# letrec
verifie
dico
=
function
[]
->
[]
|
m::l
->
if
existe
m
dico
then
verifie
dico
l
else
m::(verifie
dico
l)
;;
val verifie : arbre_lex -> string list -> string list = <fun>
# letrec
ajoute
x
=
function
[]
->
[
(x,
1
)]
|
((y
,
n)as
p)::l
->
if
x
=
ythen
(y
,
n+
1
)::lelse
p::(ajoute
x
l)
;;
val ajoute : 'a -> ('a * int) list -> ('a * int) list = <fun>
# letrec
ajoute_liste
ld
=
function
[]
->
ld
|
n::l
->
let
res
=
ajoute_liste
ld
l
in
ajoute
n
res
;;
val ajoute_liste : ('a * int) list -> 'a list -> ('a * int) list = <fun>
# letoccurences
l
=
ajoute_liste
[]
l
;;
val occurences : 'a list -> ('a * int) list = <fun>
# letorthographe
dico
nom
=
let
f
=
open_in
nom
and
res
=
ref
[]
in
try
while
true
do
let
s
=
input_line
f
in
let
ls
=
mots
s
in
let
lv
=
verifie
dico
ls
in
res
:=
ajoute_liste
!
reslv
done
;
failwith
"cas impossible"
with
End_of_file
->
close_in
f
;
!
res
|
x
->
close_in
f
;
raise
x
;;
val orthographe : arbre_lex -> string -> (string * int) list = <fun>
# letrec
est_divisible
x
=
function
[]
->
false
|
n::l
->
(x
mod
n)
=
0
||
(
(n
*
n<=
x)
&&
(est_divisible
x
l))
;;
val est_divisible : int -> int list -> bool = <fun>
On cherche le premier nombre premier à partir d'un certain entier en les testant de deux en deux.
# letrec
dernier
=
function
[]
->
failwith
"liste vide"
|
[
x]
->
x
|
_::
l->
dernier
l
;;
val dernier : 'a list -> 'a = <fun>
Et on assemble.
# letrec
plus_petit_premier
l
n
=
if
est_divisible
n
l
then
plus_petit_premier
l
(n
+
2
)else
n
;;
val plus_petit_premier : int list -> int -> int = <fun>
# letsuivant
=
function
[]
->
2
|
[
2
]
->
3
|
l
->
let
pg
=
dernier
l
in
plus_petit_premier
l
(pg
+
2
);;
val suivant : int list -> int = <fun>
# type'a
ens
=
{mutable
i
:
'a;
f
:
'a
->
'a
}
;;
type 'a ens = { mutable i: 'a; f: 'a -> 'a }
# letnext
e
=
let
x
=
e
.
iin
e
.
i
<-
(e
.
fe
.
i);
x
;;
val next : 'a ens -> 'a = <fun>
# letensprem
=
let
prec
=
ref
[
2
]
in
let
fonct
_
=
let
n
=
suivant
!
precin
prec
:=
!
prec
@
[
n]
;
n
in
{
i
=
2
;
f
=
fonct
}
;;
val ensprem : int ens = {i=2; f=<fun>}