Questions / réponses

parameters_gen

parameters_gen

von Mamadou Saliou Diallo -
Anzahl Antworten: 1

Bonjour,

je n'arrive pas à corriger ma fonction sur le moodle;

voici le message d'errer :

Erreur de type ou fonction inexistante!
--------------------------------
Error: This expression has type bool but an expression was expected of type
int


let p = fun x -> x mod 2 = 0;;
                      
let rec size p t = match t with
  |Empty -> 0
  |Bin(v,l,r) -> p v + size p l + size p r;;

let rec nb_feuille p t = match t with
  |Empty -> 0
  |Bin(v,Empty,Empty) -> p v
  |Bin(v,l,r) -> nb_feuille p l + nb_feuille p r;;

let rec arity1 p t = match t with
  |Empty -> 0
  |Bin(v,Empty,Empty) -> p v
  |Bin(v,Empty, r) -> 1 + arity1 p r
  |Bin(v,l,Empty) -> 1 + arity1 p l
  |Bin(v,l,r) ->  ( arity1 p l ) + (arity1 p r);;
                                  
let rec arity2 p t = match t with
  |Empty -> 0
  |Bin(v,Empty,Empty) -> p v
  |Bin(v,Empty, r) -> arity2 p r
  |Bin(v,l,Empty) -> arity2 p l
  |Bin(v,l,r) -> 1 + ( arity2 p l ) + (arity2 p r);;
                                       
let rec height1 p t = match t with
     Empty -> -1
    |Bin(x,Empty,Empty) -> p x
    |Bin(r,g,d) -> p r + max (height1 p g)(height1 p d);;
                            
let rec height2 p t = match t with
     Empty -> -1
    |Bin(x,Empty,Empty) -> p x
    |Bin(r,g,d) -> p r + max (height2 p g)(height2 p d);;

let rec parameters_gen p t =match t with
   |Empty ->(0,0,0,0,-1,-1)
   |Bin(x,g,d)->let s = size p t
                in
                let f = nb_feuille p t
                in
                let nA1 = arity1 p t
                in
                let nA2 = arity2 p t
                in
                let h1 = height1 p t
                in
                let h2 = height2 p t
                in (s,f,nA1,nA2,h1,h2)

cordialement.

Als Antwort auf Mamadou Saliou Diallo

Re: parameters_gen

von Philippe Duchon -

Ce serait bien de dire a quel endroit se produit l'erreur...

La fonction p est censée être un prédicat sur les clés des arbres, donc de type 'a -> bool (ou int -> bool pour les arbres d'entiers).

Dans votre fonction size, vous utilisez l'expression

p v + size p l + size p r

et donc, vous essayez d'additionner (+) des entiers (size p l, size p r) et un booléen (p v), c'est l'un des points qui provoque une erreur de type comme celle que vous signalez.

Si vous exécutez votre code dans emacs, vous aurez un message d'erreur explicite, qui vous dira quelle expression a un type bool alors que Ocaml attend un type int...