Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. #use "list_tools.ml" ;;
  2. (* exercice 1 *)
  3.  
  4. let rec is_prefix l ll = match (l,ll) with
  5. (_,[]) -> false
  6. |(l,e::l2) when prefix(l,e) = true -> true
  7. |(_,e::l2) -> is_prefix l l2 ;;
  8.  
  9. let rec decodable list = match list with
  10. [] -> true
  11. | (e::l) when is_prefix e l = true -> false
  12. | (_::l) -> decodable l ;;
  13.  
  14. (*exercice 2 *)
  15.  
  16. let rec refill (nb,obj) list = match list with
  17. [] -> (nb,obj)::list
  18. | (x,y)::l when obj = y -> (x+nb,y)::l
  19. | (x,y):: l -> (x,y)::refill (nb,obj) l
  20.  
  21. let destock (nb,obj) stock =
  22. if nb <= 0
  23. then
  24. invalid_arg " nb must be positif "
  25. else
  26. let rec stk (nb,obj) stock = match stock with
  27. [] -> stock
  28. |(x,y):: l when obj = y && nb <= x -> (x-nb,y)::l
  29. |(x,y):: l when obj = y && nb > x -> l
  30. |(x,y):: l -> (x,y):: stk (nb,obj) l
  31. in
  32. let rec stk2 (nb,obj) stock =
  33. match stock with
  34. [] -> nb
  35. |(x,y):: l when obj = y && nb <= x -> 0
  36. |(x,y):: l when obj = y && nb > x -> nb-x
  37. |(x,y):: l -> stk2 (nb,obj) l
  38. in stk(nb,obj) stock, stk2 (nb,obj) stock;;
  39.  
  40. (* exercice 3 *)
  41. let river n rank =
  42. if n <= 0
  43. then
  44. invalid_arg " n must be positif"
  45. else
  46. if rank <= 0
  47. then
  48. invalid_arg " k must be positif"
  49. else
  50. let rec riv n rank = match rank with
  51. 0 -> []
  52. | _ -> n::riv(n +sum_digits n) (rank - 1)
  53. in riv n rank ;;
  54.  
  55. let meeting n rank =
  56. let a = common (river 1 rank,river n rank)
  57. and b = common (river 3 rank,river n rank)
  58. and c = common (river 9 rank,river n rank) in
  59. if a <> 0
  60. then
  61. (1,a)
  62. else
  63. if b <> 0
  64. then
  65. (3,b)
  66. else
  67. if c <> 0
  68. then
  69. (9,c)
  70. else
  71.  
  72. (0,0) ;;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement