Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (*Paweł Zeller
- * Zadanie domowe 1, czesc 1
- * structure file
- *)
- structure id294312 :> PART_ONE =
- struct
- exception NotImplemented
- datatype 'a tree= Leaf of 'a | Node of 'a tree * 'a * 'a tree
- (*Funkcje liczb całkowitych*)
- fun sum n =
- if n = 1 then 1
- else n + sum (n-1) ;
- fun fac n =
- if n <= 1 then 1
- else fac (n-1) * n;
- fun fib n =
- if n <= 1 then 1
- else fib (n-1) + fib (n-2);
- fun gcd (n,m) =
- if n=m then n
- else if n>m then gcd (n-m, m)
- else gcd(n, m-n);
- fun max [head] = head
- | max (head::tail) =
- if head > max tail then head else max tail;
- (*Drzewa *)
- fun sumTree (Leaf n) = n
- | sumTree (Node (left, n, right)) = (sumTree left + n + sumTree right);
- fun depth (Leaf n) = 0
- | depth (Node (left, n, right)) =
- if depth left > depth right then 1 + depth left
- else 1 + depth right;
- fun binSearch (Leaf v) a =(v=a)
- | binSearch (Node(l,v,r)) a = if(v=a)then true
- else
- if(v<a) then binSearch r a
- else binSearch l a;
- fun preorder (Leaf v)=[v]
- | preorder(Node(l,v,r))=
- (v::preorder l)@(preorder r);
- (*Funkcje na listach liczb całkowitych*)
- fun listAdd l1 l2 =
- if(null l1 = true) then l2
- else if(null l2 = true) then l1
- else [hd l1 + hd l2] @ listAdd (tl l1) (tl l2);
- fun insert m l =
- if(null l = true) then [m]
- else if(m <= hd l) then [m]@l
- else hd l::insert m (tl l);
- fun insort l =
- if(length l < 2) then l
- else insert (hd l) (insort (tl l));
- (*Funkcje wyższego rzędu*)
- fun compose f g =(fn x => g(f(x)));
- fun curry f a b = f(a,b);
- fun uncurry f (a,b) = f a b;
- fun multifun f n =
- if n=1 then f
- else compose f (multifun f (n-1));
- (*Funkcje na liście*)
- fun ltake l i =
- if null l=true then [] else
- if (i=0) then []
- else (hd l :: ltake (tl l) (i-1) );
- fun lall f [x] =
- f x
- | lall f l =
- if f (hd l) then lall f (tl l)
- else false;
- fun lmap f []=
- []
- | lmap f l = ( f (hd l) :: lmap f (tl l) );
- fun lrev [] = []
- | lrev l = ( lrev (tl l) )@[hd l];
- fun lzip ([],l2)=[]
- | lzip(l1,[])=[]
- | lzip(l1,l2)= ((hd l1), (hd l2)):: lzip( tl l1, tl l2);
- fun split [] = ([],[])
- |split [x] = ([x],[])
- |split (head1::head2::tail) =
- let val (t1,t2) = split tail
- in ((head1::t1),(head2::t2)) end;
- fun cartprod l [] =[]
- | cartprod [] l =[]
- | cartprod l1 l2 = (hd l1, hd l2)::(cartprod [hd l1] (tl l2) )@(cartprod (tl l1) l2);
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement