Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #use "list_tools.ml" ;;
- (* exercice 1 *)
- let rec is_prefix l ll = match (l,ll) with
- (_,[]) -> false
- |(l,e::l2) when prefix(l,e) = true -> true
- |(_,e::l2) -> is_prefix l l2 ;;
- let rec decodable list = match list with
- [] -> true
- | (e::l) when is_prefix e l = true -> false
- | (_::l) -> decodable l ;;
- (*exercice 2 *)
- let rec refill (nb,obj) list = match list with
- [] -> (nb,obj)::list
- | (x,y)::l when obj = y -> (x+nb,y)::l
- | (x,y):: l -> (x,y)::refill (nb,obj) l
- let destock (nb,obj) stock =
- if nb <= 0
- then
- invalid_arg " nb must be positif "
- else
- let rec stk (nb,obj) stock = match stock with
- [] -> stock
- |(x,y):: l when obj = y && nb <= x -> (x-nb,y)::l
- |(x,y):: l when obj = y && nb > x -> l
- |(x,y):: l -> (x,y):: stk (nb,obj) l
- in
- let rec stk2 (nb,obj) stock =
- match stock with
- [] -> nb
- |(x,y):: l when obj = y && nb <= x -> 0
- |(x,y):: l when obj = y && nb > x -> nb-x
- |(x,y):: l -> stk2 (nb,obj) l
- in stk(nb,obj) stock, stk2 (nb,obj) stock;;
- (* exercice 3 *)
- let river n rank =
- if n <= 0
- then
- invalid_arg " n must be positif"
- else
- if rank <= 0
- then
- invalid_arg " k must be positif"
- else
- let rec riv n rank = match rank with
- 0 -> []
- | _ -> n::riv(n +sum_digits n) (rank - 1)
- in riv n rank ;;
- let meeting n rank =
- let a = common (river 1 rank,river n rank)
- and b = common (river 3 rank,river n rank)
- and c = common (river 9 rank,river n rank) in
- if a <> 0
- then
- (1,a)
- else
- if b <> 0
- then
- (3,b)
- else
- if c <> 0
- then
- (9,c)
- else
- (0,0) ;;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement