Advertisement
Guest User

Untitled

a guest
Feb 1st, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 4.45 KB | None | 0 0
  1. (* Assignment 1 *) (* Do not edit this line. *)
  2. (* Student name: Andi Rayhan Chibrandy, Id Number: 260567400 *) (* Edit this line. *)
  3.  
  4. (* In the template below we have written the names of the functions that
  5. you need to define.  You MUST use these names.  If you introduce auxiliary
  6. functions you can name them as you like except that your names should not
  7. clash with any of the names we are using.  We have also shown the types
  8. that you should have.  Your code must compile and must not go into infinite
  9. loops.  *)
  10.  
  11.  
  12. let rec slhelper (l:float list) acc:float =
  13.   match l with
  14.   | [] -> acc
  15.   | x::rest -> slhelper rest (x+acc)
  16.  
  17. let rec sqlisthelper (l:float list) (acc:float list)=
  18.   match l with
  19.   | [] -> acc
  20.   | x::rest -> sqlisthelper rest (acc@[(x*x)])
  21.  
  22. let rec listlength (l:float list) (acc:float) =
  23.   match l with
  24.   | [] -> acc
  25.   | x::rest -> listlength rest (acc+1.0)
  26.  
  27. let rec mdhelper (l:float list) (mean:float) (acc:float list) =
  28.   match l with
  29.   | [] -> acc
  30.   | x::xs -> mdhelper xs mean (acc@[(x-mean)])
  31.  
  32.  
  33.  
  34.  
  35. (* Question 1 *) (* Do not edit this line. *)
  36.  
  37. (* val sumlist : l:float list -> float *)
  38. let rec sumlist l = slhelper l 0.0(* remove failwith and replace with your code *)
  39.  
  40. (* val squarelist : l:float list -> float list *)
  41. let rec squarelist l = (* remove failwith and replace with your code *)
  42.   sqlisthelper l []
  43.  
  44. (* val mean : l:float list -> float *)
  45. let mean l = (sumlist l)/(listlength l 0.0)(* remove failwith and replace with your code *)
  46.  
  47. (* val mean_diffs : l:float list -> float list *)
  48. let mean_diffs l = mdhelper l (mean l) [](* remove failwith and replace with your code *)
  49.  
  50. (* val variance : l:float list -> float *)
  51. let variance l = (* remove failwith and replace with your code *)
  52.   (sumlist (squarelist (mean_diffs l)))/(listlength l 0.0)
  53.  
  54. (* End of question 1 *) (* Do not edit this line. *)
  55.  
  56. (* Question 2 *) (* Do not edit this line. *)
  57.  
  58. (* val memberof : 'a * 'a list -> bool when 'a : equality *)
  59. let rec memberof l = (* remove failwith and replace with your code *)
  60.   let rec loop (l, list) =
  61.     match list with
  62.     | [] -> false
  63.     | x::xs ->
  64.       if x = l then true
  65.       else loop(l, xs)
  66.   match l with
  67.   | (elem, list) -> loop (elem, list)
  68.  
  69.  
  70. (* val remove : 'a * 'a list -> 'a list when 'a : equality *)
  71. let rec remove l = (* remove failwith and replace with your code *)
  72.   let rec loop elem list dummy =
  73.     match list with
  74.     | [] -> dummy
  75.     | x::xs ->
  76.       if x = elem then loop elem xs dummy
  77.       else loop elem xs (dummy@[x])
  78.   match l with
  79.   | (elem, l1) -> loop elem l1 []
  80.  
  81. (* End of question 2 *) (* Do not edit this line *)
  82.  
  83. (* Question 3 *) (* Do not edit this line *)
  84.  
  85. (* val isolate : l:'a list -> 'a list when 'a : equality *)
  86. let rec isolate l = (* remove failwith and replace with your code *)
  87.   let rec loop l1 dummy =
  88.     match l1 with
  89.     | [] -> dummy
  90.     | x::xs ->
  91.       if (memberof(x, dummy)) then loop xs dummy
  92.       else loop xs (dummy@[x])
  93.   loop l []
  94.  
  95. (* End of question 3 *) (* Do not edit this line *)
  96.  
  97. (* Question 4 *) (* Do not edit this line *)
  98.  
  99. (* val common : 'a list * 'a list -> 'a list when 'a : equality *)
  100. let rec common l = (* remove failwith and replace with your code *)
  101.   let rec loop la lb dummy =
  102.     match la with
  103.     | [] -> dummy
  104.     | x::rest ->
  105.       if memberof(x, lb) && not (memberof(x, dummy))
  106.       then loop rest lb (dummy@[x])
  107.       else loop rest lb dummy
  108.   match l with
  109.   | (l1, l2) -> loop l1 l2 []
  110.  
  111.  
  112. (* End of question 4 *) (* Do not edit this line *)
  113.  
  114. (* Question 5 *) (* Do not edit this line *)
  115.  
  116. (* val split : l:'a list -> 'a list * 'a list *)
  117. let rec split l = (* remove failwith and replace with your code *)
  118.   let rec loop l1 (l2,l3) =
  119.     match l1 with
  120.     | [] -> (l2,l3)
  121.     | x::rest -> loop rest (l3@[x], l2)
  122.   loop l ([],[])
  123.  
  124. (* val merge : 'a list * 'a list -> 'a list when 'a : comparison *)
  125. let rec merge l = (* remove failwith and replace with your code *)
  126.   let rec loop (l1,l2) =
  127.     match (l1,l2) with
  128.     | (xs,[]) | ([],xs) -> xs
  129.     | (x::tx, y::ty) ->
  130.       if x < y then x::loop(tx, l2)
  131.       else y::loop(l1, ty)
  132.   match l with
  133.   | (l1, l2) -> loop(l1, l2)
  134.  
  135. (* val mergesort : l:'a list -> 'a list when 'a : comparison *)
  136. let rec mergesort l = (* remove failwith and replace with your code *)
  137.   match l with
  138.   | [] -> []
  139.   | [x] -> [x]
  140.   | _ ->
  141.     let (l1,l2) = split l in
  142.     merge ((mergesort l1),(mergesort l2))
  143.  
  144. (* End of question 5 *) (* Do not edit this line *)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement