Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. structure foo :> PART_ONE =
  2. struct
  3. exception NotImplemented
  4.  
  5. datatype 'a tree= Leaf of 'a | Node of 'a tree * 'a * 'a tree
  6.  
  7. fun sum n =
  8. if n = 1 then 1
  9. else sum (n-1) + n
  10.  
  11. fun fac n =
  12. if n = 0 then 1
  13. else fac (n-1) * n
  14.  
  15. fun fib n =
  16. if n = 1 then 1
  17. else if n = 2 then 2
  18. else fib (n-1) + fib (n-2)
  19.  
  20. fun gcd (n, m) =
  21. if n = m then n
  22. else if n > m then gcd (n-m, m)
  23. else gcd (n, m-n)
  24.  
  25. fun max (l:int list) =
  26. case l of
  27. nil => 0
  28. | head :: tail =>
  29. if tail = [] orelse head > max tail
  30. then head
  31. else max tail
  32.  
  33. fun sumTree (t:int tree) =
  34. case t of
  35. Leaf l => l
  36. | Node (l,v,r) => v + sumTree (l) + sumTree (r)
  37.  
  38. fun depth (t:'a tree) =
  39. case t of
  40. Leaf l => 0
  41. | Node (l, _, p) =>
  42. 1 + (fn (x,y) => if x>y then x else y)(depth l,depth p);
  43.  
  44. fun binSearch (t:int tree) (x:int) =
  45. case t of
  46. Leaf l => l=x
  47. | Node (l,v,r) =>
  48. if x=v then true
  49. else if x<v then binSearch l x
  50. else binSearch r x
  51.  
  52. fun preorder (t:'a tree) =
  53. case t of
  54. Leaf l => [l]
  55. | Node(l,v,r) => [v] @ preorder l @ preorder r
  56.  
  57. fun listAdd [] (b:int list) = b
  58. | listAdd (a:int list) [] = a
  59. | listAdd (a:int list as ha :: ta) (b:int list as hb :: tb) =
  60. (ha + hb) :: listAdd ta tb
  61.  
  62. fun insert (m:int) [] = [m]
  63. | insert (m:int) (l:int list as h::t) =
  64. if m < h then m :: l
  65. else h :: insert m t
  66.  
  67. fun insort (l:int list) =
  68. let
  69. fun sort [] res = res
  70. | sort (h::t) res = sort t (insert h res)
  71. in
  72. sort l nil
  73. end
  74.  
  75.  
  76. fun compose f g = (fn x=> g (f x))
  77.  
  78. fun curry f garek majecki = f(garek,majecki)
  79.  
  80. fun uncurry f (x,y) = f x y
  81.  
  82. fun multifun f n =
  83. if n = 1 then (fn x => f x)
  84. else (fn x =>f ( (multifun f (n-1)) x ))
  85.  
  86. fun ltake _ 0 = []
  87. |ltake [] _ = []
  88. |ltake (x::xs) n = x::(ltake xs (n-1))
  89.  
  90. fun lall _ [] = true
  91. |lall f (x::xs)= if (f x) then (lall f xs) else false
  92.  
  93. fun lmap _ [] = []
  94. |lmap f (x::xs)= (f x)::(lmap f xs)
  95.  
  96. fun lrev [] = []
  97. |lrev (x::xs) = (lrev xs) @ [x]
  98.  
  99. fun lzip ([],_) = []
  100. |lzip (_,[]) = []
  101. |lzip ((x::xs),(y::ys)) = (x,y)::(lzip (xs,ys))
  102.  
  103. fun split [] = ([],[])
  104. |split [x] = ([x],[])
  105. |split (x1::x2::xs) = let val (x1s,x2s) = split xs
  106. in ((x1::x1s),(x2::x2s)) end;
  107. fun cartprod _ [] =[]
  108. |cartprod [] _ = []
  109. |cartprod (x::xs) ys = (lmap (fn y=> (x,y)) ys) @ (cartprod xs ys)
  110.  
  111. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement