Advertisement
Bobita

LAB11_LISP

May 10th, 2023
1,424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 1.21 KB | Source Code | 0 0
  1. (*LAB_11*)
  2.  
  3. (*[#EX_1]*)
  4. let fibbonaci n =
  5.   let rec acc n b a =
  6.     if n<= 0 then a
  7.     else acc (n-1) (a+b) b in
  8.   acc n 1 0  ;;
  9.  
  10. # fibbonaci 9 ;;
  11. - : int = 34
  12.  
  13. (*[#EX_2]*)
  14. let rec ackermann =
  15.   function x,y ->
  16.     if x = 0 then y+1
  17.     else if y = 0 then ackermann (x-1, 1)
  18.     else ackermann (x-1, ackermann (x, y-1)) ;;
  19.    
  20. (*[#EX_3]*)
  21. let rec geninterval s e =
  22.   if s > e then []
  23.   else s :: geninterval (s + 1) e
  24. ;;
  25.  
  26. geninterval 3 8;;
  27.  
  28. (*[#EX_4]*)
  29. let my_not = function number ->
  30.   if number then true
  31.   else false ;;
  32. my_not(5=2);;
  33.  
  34. let my_and = function x, y ->
  35.   if x = false then false
  36.   else if y = false then false
  37.   else true ;;
  38.  
  39. let my_or = function x, y ->
  40.   if x = true then true
  41.   else if y = true then true
  42.   else false ;;
  43.  
  44. my_and(5=5, 4=4);;
  45. my_or(5=5, 4=4);;
  46.  
  47. (*[#EX_5]*)
  48. let rec digits = function nr ->
  49.   if (nr/10=0) then nr::[]
  50.   else digits ( nr/10)@[nr mod 10] ;;
  51.  
  52. digits 543;;
  53.  
  54. (*[#EX_6]*)
  55. let rec maxim lista =
  56.   if List.length lista = 1 then List.hd lista
  57.   else max( maxim (List.tl lista)) (List.hd lista);;
  58.  
  59. (*[#EX_7]*)
  60. let rec cmmdc x y =
  61.   if (x=y) then x else
  62.   if (x>y) then cmmdc (x-y) y
  63.   else cmmdc x (y-x);;
  64.  
  65. cmmdc 5 4 ;;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement