SHARE
TWEET

Untitled

a guest Nov 15th, 2019 114 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Q0 AssertEquality using match…with
  2. let AssertEquality inputTuple =
  3.   match inputTuple with
  4.   | (a,b) when a=b -> printfn " Test Success"
  5.   | _ -> printfn " Test Fail"
  6.  
  7. //NOTE: all the below questions require Tail Recursion + Match with
  8. //Q1 remember the example from PPT: grab the even numbers out from a list
  9. let rec getEven mylis accumulator=
  10.   match mylis with
  11.   | [] -> accumulator
  12.   | head::tail when head%2=0 -> getEven tail (head::accumulator)
  13.   | _::tail -> getEven tail accumulator
  14. let mylis=[1..6]
  15. let result = getEven mylis []
  16. printf "Q1..."
  17. AssertEquality (result, [6;4;2])
  18. let result1 = result |> getEven <| []
  19. AssertEquality (result1, [2;4;6])
  20.  
  21. //make sure the recursive function call is the VERY LAST step of your calculation to avoid stack overflow
  22. //Always use a result accumulator to remember the result of each step
  23. //getEven tail (head::accumulator) instead of head::getEven tail
  24.  
  25. //Q2 modify Q1 so that you can put the reverse the result list in the function call directly
  26. let rec getEven2 mylis accumulator=
  27.   let getEvenInner mylis acc=
  28.     match mylis with
  29.     | [] -> acc
  30.     | head::tail when head%2=0 -> getEven2  tail (head::acc)
  31.     | _::tail -> getEven2 tail acc
  32.   (List.rev (getEvenInner mylis accumulator))
  33. let result2=getEven2 mylis []
  34. printf "Q2..."
  35. AssertEquality (result2, [2;4;6])
  36.  
  37. //Q3 modify Q2 and create the below function which will grab the odd numbers out from a list
  38. let rec getOdd lis accumulator =
  39.   let getOddinner lis acc =
  40.     match lis with
  41.     | [] -> accumulator
  42.     | head::tail when head%2=1 -> getOdd tail (head::acc)
  43.     | _::tail -> getOdd tail acc
  44.   (List.rev(getOddinner lis accumulator))
  45. let result3=getOdd mylis []
  46. printf "Q3..."
  47. AssertEquality (result3, [1;3;5])
  48.  
  49. //Q4 Get the sum of all the even numbers in a list
  50. let rec getEvenSum mylis sumAcc=
  51.   match mylis with
  52.   | [] -> sumAcc
  53.   | head::tail when head%2=0 -> getEvenSum tail (sumAcc+head)
  54.   | _::tail -> getEvenSum tail sumAcc
  55.  
  56. let result4=getEvenSum mylis 0
  57. printf "Q4..."
  58. AssertEquality (result4, 12)
  59.  
  60. //Q5 calculate the square of each number in the input list
  61. let rec getSqr mylis sqrLis=
  62.   let rec getSqrInner mylis sqrLis=
  63.     match mylis with
  64.     | [] -> sqrLis
  65.     | head::tail -> getSqr tail ((head*head)::sqrLis)
  66.   (List.rev(getSqrInner mylis sqrLis))
  67. let result5=getSqr mylis []
  68. printf "Q5..."
  69. AssertEquality (result5, [1;4;9;16;25;36])
  70.  
  71. //Q6 Calculate factorial of n
  72. //n! = 1*2*3*...*(n-1)*n
  73. let rec factorial n ftRes=
  74.   match n with
  75.   | 0 -> 1
  76.   | 1 -> 1
  77.   | _ -> n * factorial (n-1) 1
  78.  
  79. let result6=factorial 5 1
  80. printf "Q6..."
  81. AssertEquality (result6, 120)
  82.  
  83. //Q7 You can merge the 2 "if statements" of the pattern 0 and 1 together like below:
  84. let rec factorial1 n ftRes=
  85.   match n with
  86.   | 0 | 1 -> 1
  87.   | _ -> n * factorial1 (n-1) 1
  88.  
  89. let result7=factorial1 5 1
  90. printf "Q7..."
  91. AssertEquality (result7, 120)
  92.  
  93. //Q8 to help user call this function easier by using "factorial 5" instead of "factorial1 5 1"
  94. //(so that the API user do not need to worry about giving us the accumulator as the second func input),
  95. //we can wrap the recursive function call inside of regular function that looks nicer
  96. let nicer_factorial n =
  97.   let rec factorial1 n ftRes=
  98.     match n with
  99.     | 0 | 1 -> 1
  100.     | _ -> n * factorial1 (n-1) ftRes
  101.   factorial1 n 1
  102.  
  103. let result8=nicer_factorial 5
  104. printf "Q8..."
  105. AssertEquality (result8, 120)
  106.  
  107.  
  108. //////////////////////////////////////////////////////////
  109.  
  110. // queue definition stuff
  111. type Queue = QueueContents of int list
  112.  
  113. let EMPTY = QueueContents []
  114.  
  115. let enqueue x q =
  116.   let (QueueContents contents) = q
  117.   let newContents = List.append contents [x]
  118.   QueueContents newContents
  119.  
  120. let dequeue (QueueContents contents) =
  121.   match contents with
  122.   | head::tail ->
  123.     let newQueue = QueueContents tail
  124.     (contents.Head, QueueContents contents.Tail)
  125.   | [] -> failwith "Queue is Empty"
  126.  
  127. // create random number jobs
  128. let createJobs count =
  129.     let rnd = System.Random()
  130.     List.init count (fun _->rnd.Next(1,9))
  131.  
  132. // parse list
  133. let rec workJobs jobqueue joblis=
  134.   match joblis with
  135.   | [] -> printfn "Reached end of available jobs"
  136.   | head::tail when head%2=1 ->
  137.     printf "New job %d added to Queue.\n" head
  138.     workJobs (enqueue head jobqueue) tail
  139.   | head::tail when head%2=0 ->
  140.     try
  141.       let front, newQueue = dequeue jobqueue
  142.       printf "Completed job %d.\n" front
  143.       workJobs newQueue tail
  144.     with
  145.     | Failure(msg) -> printfn "No more jobs to do right now."
  146.     workJobs jobqueue tail
  147.     | _::tail -> workJobs jobqueue tail
  148.  
  149. let main =
  150.   let jobs = createJobs 10
  151.   printfn "%A" jobs
  152.   workJobs EMPTY jobs
  153.  
  154. main
  155.  
  156. (*
  157. let testQ = EMPTY |> enqueue 1 |> enqueue 2
  158. printf "%A" testQ
  159. let front, testQ2 = dequeue testQ
  160. printf "%d, %A" front testQ2
  161. let testQ3 = enqueue 3 testQ
  162. printf "%A" testQ3
  163. *)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top