Bonjour,
J'ai un programme qui gère un alphabet :
Ce code fonctionne.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75 (**Alphabet*) type lettre = GrandF | PetitF | Plus | Moins | CrochetO | CrochetF;; (**Type mot*) type mot = lettre list;; (**Regle Production : une fonction qui transforme une lettre en mot*) type production = lettre -> mot;; (**lsystem : une association d'un mot et d'une liste de regles de production*) type lsystem = { mot_initial: mot; regle: production list };; exception LettreInconnue;; (** transforme une lettre en string*) let string_of_lettre lettre = match lettre with | GrandF -> "F" | PetitF -> "f" | Plus -> "+" | Moins -> "-" | CrochetO -> "[" | CrochetF -> "]";; (**transforme une string en lettre*) let lettre_of_string st = match st with | "F" -> GrandF | "f" -> PetitF | "+" -> Plus | "-" -> Moins | "[" -> CrochetO | "]" -> CrochetF | _ -> raise LettreInconnue;; (**transforme un mot en string*) let rec string_of_mot mot = match mot with | [] -> "" | a::suite -> (string_of_lettre a)^(string_of_mot suite);; (**transforme un string en mot*) let rec mot_of_string st = match st with | "" -> [] | _ -> (lettre_of_string (String.sub st 0 1)) :: (mot_of_string (String.sub st 1 ((String.length st)-1)));; (**transforme un string en production*) let production_of_string st = let s1 = String.sub st 0 1 in let s2 = String.sub st 1 2 in let s3 = String.sub st 3 ((String.length st)-3) in if s2<> "->" then failwith "Erreur de chaine" else let ss1 = lettre_of_string s1 in let ss3 = mot_of_string s3 in (fun x -> match x with | a when a = ss1 -> ss3 | a -> a :: []);; (**transforme une liste de string en liste de production*) let rec listprod_of_listst listst = match listst with |[] -> [] |a::s -> (production_of_string a) :: (listprod_of_listst s);; (**transforme un string en lsystem*) let lsystem_of_string st listst = let f1 = (mot_of_string st) in let f2 = (listprod_of_listst listst) in { mot_initial = f1; regle = f2 };; (**applique la regle de production sur le mot donné*) let rec applique_regle reg mot = match mot with |[] ->[] |a::s -> (reg a) @ (applique_regle reg s);; (**dérive le lsystem donné en appliquant ses regles de productions*) let rec derive lsystem = List.fold_left (fun mot regle -> applique_regle regle mot) lsystem.mot_initial lsystem.regle;;
Cependant, la suite de mon programme doit afficher certaines choses pour un mot donné :
on considere une tortue qui avance sur notre fenêtre graphique.
Un F fait avancer la tortue d'une distance pas en tracant un trait.
Un f fait avancer la tortue d'une distance pas sans tracer de trait.
Un + fait tourner la tortue d'un angle +delta
Un - fait tourner la tortue d'un angle -delta
Un [ sauvegarde la position courante de la tortue
Un ] restaure la position de la tortue à son état lors du précédent [
J'ai tenté un petit quelque-chose mais étant débutant (je n'avais jamais fait de caml, je viens d'arriver dans le cycle IUP apres un DUT) cela ne fonctionne pas. quelqu'un pourrait me le corriger ?
Rien ne se passe dans ma fenêtre lorsque j'execute ceci dans ma boucle itérative.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 #load "graphics.cma";; let pas=35;; let x=320;; let y=0;; let delta=45;; let ymemo=y;; let xmemo=x;; let angle=0;; (** Angle représente l'angle d'inclinaison de la tortue *) let trace_lettre lettre = match lettre with | GrandF -> begin Graphics.lineto x (y+pas); y=y+pas end | PetitF -> y=y+pas | Plus -> angle=angle+delta | Moins -> angle=angle-delta | CrochetO -> xmemo=x | CrochetF -> ymemo=y ;; let trace lsystem = match lsystem.mot_initial with |a::suite-> trace_lettre a |[]->true;; let mot=[PetitF;Plus;GrandF;Moins;CrochetO;CrochetF];; let prod1=production_of_string "f->++[]";; let prod2=production_of_string "F->[-[";; let lsyst={mot_initial=mot;regle=[prod1;prod2]};; Graphics.open_graph "";; Graphics.set_color (Graphics.black);; trace lsyst;;
Merci d'avance
mavina
edit : si ce message n'est pas à sa place, merci aux modérateurs de bien vouloir le déplacer à l'endroit approprié.
Partager