Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. let rec length list =
  2. match list with
  3. [] -> 0
  4. |e::l -> 1 + length l ;;
  5.  
  6. let rec append x y = if y = []
  7. then
  8. x
  9. else
  10. let rec appendlist = function
  11. [] -> y
  12. |e::l -> e::appendlist l
  13. in appendlist x ;;
  14.  
  15. let rec couplesize list = match list with
  16. [] -> 0
  17. |(x,y)::l -> x + couplesize l ;;
  18.  
  19. let nth n list =
  20. if n < 0 then
  21. invalid_arg "nth: index must be a natural"
  22. else
  23. let rec nth_rec = function
  24. (_, []) -> failwith "nth: list is too short"
  25. | (1, e::l) -> e
  26. | (i, e::l) -> nth_rec (i-1, l)
  27. in
  28. nth_rec (n+1, list) ;;
  29.  
  30. let search_pos n list =
  31. let rec searchp n list i = match list with
  32. [] -> failwith "shorsearch_pos: not found"
  33. | e ::l when n=e -> i
  34. | _ :: l -> searchp n l i+1
  35. in
  36. searchp n list 0 ;;
  37.  
  38. let rec sum_digits n = match n with
  39. n when n=0 -> 0
  40. | _ -> n mod 10 + sum_digits (n/10) ;;
  41.  
  42. let rec common (list1,list2) = match (list1,list2) with
  43. | ([],_) -> 0
  44. | (_,[]) -> 0
  45. |((e1::l1),(e2::l2)) when e1=e2 -> e1
  46. |((e1::l1),(e2::l2)) when e1 < e2 -> common (e2::l2,e1::l1)
  47. |((list1,_::l2)) -> common (l2,list1) ;;
  48.  
  49.  
  50. let rec prefix (list1,list2) = match (list1,list2) with
  51. ([],_) -> true
  52. | (_,[]) -> true
  53. | (e1::l1,e2::l2) when e1=e2 -> prefix (l1,l2)
  54. | (e1::l1,e2::l2) -> false;;
  55.  
  56. let init_list n x = if n < 0
  57. then
  58. invalid_arg "init_list: n must be a natural"
  59. else
  60. let rec init n x = match n with
  61. 0 -> []
  62. |_ -> x:: init (n-1) x
  63. in
  64. init n x ;;
  65.  
  66. let rec put_list n x list = match (x,list) with
  67. (_,[]) -> list
  68. | (0,e::l) -> n::l
  69. | (i,e::l) -> e:: put_list n (x-1) l ;;
  70.  
  71.  
  72. let rec length list =
  73. match list with
  74. [] -> 0
  75. |e::l -> 1 + length l ;;
  76.  
  77. let rec append x y = if y = []
  78. then
  79. x
  80. else
  81. let rec appendlist = function
  82. [] -> y
  83. |e::l -> e::appendlist l
  84. in appendlist x ;;
  85.  
  86. let rec couplesize list = match list with
  87. [] -> 0
  88. |(x,y)::l -> x + couplesize l ;;
  89.  
  90. let nth n list =
  91. if n < 0 then
  92. invalid_arg "nth: index must be a natural"
  93. else
  94. let rec nth_rec = function
  95. (_, []) -> failwith "nth: list is too short"
  96. | (1, e::l) -> e
  97. | (i, e::l) -> nth_rec (i-1, l)
  98. in
  99. nth_rec (n+1, list) ;;
  100.  
  101. let search_pos n list =
  102. let rec searchp n list i = match list with
  103. [] -> failwith "shorsearch_pos: not found"
  104. | e ::l when n=e -> i
  105. | _ :: l -> searchp n l i+1
  106. in
  107. searchp n list 0 ;;
  108.  
  109. let rec sum_digits n = match n with
  110. n when n=0 -> 0
  111. | _ -> n mod 10 + sum_digits (n/10) ;;
  112.  
  113. let rec common (list1,list2) = match (list1,list2) with
  114. | ([],_) -> 0
  115. | (_,[]) -> 0
  116. |((e1::l1),(e2::l2)) when e1=e2 -> e1
  117. |((e1::l1),(e2::l2)) when e1 < e2 -> common (e2::l2,e1::l1)
  118. |((list1,_::l2)) -> common (l2,list1) ;;
  119.  
  120.  
  121. let rec prefix (list1,list2) = match (list1,list2) with
  122. ([],_) -> true
  123. | (_,[]) -> true
  124. | (e1::l1,e2::l2) when e1=e2 -> prefix (l1,l2)
  125. | (e1::l1,e2::l2) -> false
  126.  
  127. let init_list n x = if n < 0
  128. then
  129. invalid_arg "init_list: n must be a natural"
  130. else
  131. let rec init n x = match n with
  132. 0 -> []
  133. |_ -> x:: init (n-1) x
  134. in
  135. init n x ;;
  136.  
  137. let rec put_list n x list = match (x,list) with
  138. (_,[]) -> list
  139. | (0,e::l) -> n::l
  140. | (i,e::l) -> e:: put_list n (x-1) l ;;
  141.  
  142. let init_board (l,c) v = init_list l (init_list c v);;
  143.  
  144. let get_cell (x,y) board = nth y (nth x (board));;
  145.  
  146.  
  147. let init_board (l,c) v = init_list l (init_list c v);;
  148.  
  149. let get_cell (x,y) board = nth y (nth x (board));;
  150.  
  151.  
  152. let put_cell v (x,y) b =
  153. put_list (put_list v y (nth x b)) x b ;;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement