Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. (*Paweł Zeller
  2. * Zadanie domowe 1, czesc 1
  3. * structure file
  4. *)
  5. structure id294312 :> PART_ONE =
  6. struct
  7. exception NotImplemented
  8.  
  9. datatype 'a tree= Leaf of 'a | Node of 'a tree * 'a * 'a tree
  10.  
  11.  
  12. (*Funkcje liczb całkowitych*)
  13. fun sum n =
  14. if n = 1 then 1
  15. else n + sum (n-1) ;
  16.  
  17. fun fac n =
  18. if n <= 1 then 1
  19. else fac (n-1) * n;
  20.  
  21. fun fib n =
  22. if n <= 1 then 1
  23. else fib (n-1) + fib (n-2);
  24.  
  25. fun gcd (n,m) =
  26. if n=m then n
  27. else if n>m then gcd (n-m, m)
  28. else gcd(n, m-n);
  29.  
  30. fun max [head] = head
  31. | max (head::tail) =
  32. if head > max tail then head else max tail;
  33.  
  34. (*Drzewa *)
  35. fun sumTree (Leaf n) = n
  36. | sumTree (Node (left, n, right)) = (sumTree left + n + sumTree right);
  37.  
  38. fun depth (Leaf n) = 0
  39. | depth (Node (left, n, right)) =
  40. if depth left > depth right then 1 + depth left
  41. else 1 + depth right;
  42.  
  43. fun binSearch (Leaf v) a =(v=a)
  44. | binSearch (Node(l,v,r)) a = if(v=a)then true
  45. else
  46. if(v<a) then binSearch r a
  47. else binSearch l a;
  48.  
  49. fun preorder (Leaf v)=[v]
  50. | preorder(Node(l,v,r))=
  51. (v::preorder l)@(preorder r);
  52.  
  53.  
  54. (*Funkcje na listach liczb całkowitych*)
  55. fun listAdd l1 l2 =
  56. if(null l1 = true) then l2
  57. else if(null l2 = true) then l1
  58. else [hd l1 + hd l2] @ listAdd (tl l1) (tl l2);
  59.  
  60. fun insert m l =
  61. if(null l = true) then [m]
  62. else if(m <= hd l) then [m]@l
  63. else hd l::insert m (tl l);
  64.  
  65. fun insort l =
  66. if(length l < 2) then l
  67. else insert (hd l) (insort (tl l));
  68.  
  69. (*Funkcje wyższego rzędu*)
  70. fun compose f g =(fn x => g(f(x)));
  71.  
  72. fun curry f a b = f(a,b);
  73.  
  74. fun uncurry f (a,b) = f a b;
  75.  
  76. fun multifun f n =
  77. if n=1 then f
  78. else compose f (multifun f (n-1));
  79.  
  80. (*Funkcje na liście*)
  81. fun ltake l i =
  82. if null l=true then [] else
  83. if (i=0) then []
  84. else (hd l :: ltake (tl l) (i-1) );
  85.  
  86. fun lall f [x] =
  87. f x
  88. | lall f l =
  89. if f (hd l) then lall f (tl l)
  90. else false;
  91.  
  92. fun lmap f []=
  93. []
  94. | lmap f l = ( f (hd l) :: lmap f (tl l) );
  95.  
  96. fun lrev [] = []
  97. | lrev l = ( lrev (tl l) )@[hd l];
  98.  
  99. fun lzip ([],l2)=[]
  100. | lzip(l1,[])=[]
  101. | lzip(l1,l2)= ((hd l1), (hd l2)):: lzip( tl l1, tl l2);
  102.  
  103. fun split [] = ([],[])
  104. |split [x] = ([x],[])
  105. |split (head1::head2::tail) =
  106. let val (t1,t2) = split tail
  107. in ((head1::t1),(head2::t2)) end;
  108.  
  109. fun cartprod l [] =[]
  110. | cartprod [] l =[]
  111. | cartprod l1 l2 = (hd l1, hd l2)::(cartprod [hd l1] (tl l2) )@(cartprod (tl l1) l2);
  112.  
  113.  
  114.  
  115. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement