problem with type in ocaml -


i have defined type :

  type state= l of state_simple * int | n of state * int |  state of int  

if need integer of "state" should do?

here code:

   let prove state ch =    (*"state" of type state st, need integer st*)    let (_,p,suc,_) =game.(i) in     let x = ref[] in    let y = ref(variable a.(suc.(0)) )in    let l = array.length suc in      x :=a.(suc.(0)) :: !x;     if (p=0)        (if (l <> 1)            (for i=1 l-1                 x := ((a.(suc.(i))) :: !x)            done;    !x;; 

i first recommend trying better understand immutability , functional techniques don't need references lot of doing. here how integer:

let prove st ch =    let = match st    | state x -> x    | l _ | n _ -> assert false (* or raise exception *)   in    let (_,p,suc,_) =game.(i) in     let x = ref[] in    let y = ref(variable a.(suc.(0)) )in (* using y anywhere? *)    let l = array.length suc in      x :=a.(suc.(0)) :: !x;     if (p=0)        (if (l <> 1)            (for i=1 l-1                 x := ((a.(suc.(i))) :: !x)            done;    !x;; 

you don't seem using y, i'm not sure if that's due typo or else. can construct list x functionally using recursion:

let prove st ch =    let = match st    | state x -> x    | l _ -> assert false (* or raise exception *)    | n _ -> assert false   in    let (_,p,suc,_) =game.(i) in     let l = array.length suc in      let rec loop x lst =      if x >= l        lst      else        loop (x+1) (a.(suc.(i)) :: lst)    in    if (p=0) && (l <> 1)       loop 1 [a.(suc.(0))]    else      [] 

edit: after reading through comments, sounds confused constitutes type in ocaml.

type state= l of state_simple * int | n of state * int |  state of int 

creates new type called state. state(2) , n(state(3), 2) have same type, different values. if write function signature val f : state -> int (that is, function named f takes state , returns int), can pass function state(2) or n(n(state(3), 4), 2) or else.

since want function prove accept state value state(x), might want rethink way calling prove. maybe prove should take int instead of state, , caller of prove can pattern matching.

if cumbersome (prove called in multiple places) having match statement in function makes sense, long bad matches (l_ , 'n_') handled properly.


Comments

Popular posts from this blog

Add email recipient to all new Trac tickets -

400 Bad Request on Apache/PHP AddHandler wrapper -

php - Change action and image src url's with jQuery -