Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Q0 AssertEquality using match…with
- let AssertEquality inputTuple =
- match inputTuple with
- | (a,b) when a=b -> printfn " Test Success"
- | _ -> printfn " Test Fail"
- //NOTE: all the below questions require Tail Recursion + Match with
- //Q1 remember the example from PPT: grab the even numbers out from a list
- let rec getEven mylis accumulator=
- match mylis with
- | [] -> accumulator
- | head::tail when head%2=0 -> getEven tail (head::accumulator)
- | _::tail -> getEven tail accumulator
- let mylis=[1..6]
- let result = getEven mylis []
- printf "Q1..."
- AssertEquality (result, [6;4;2])
- let result1 = result |> getEven <| []
- AssertEquality (result1, [2;4;6])
- //make sure the recursive function call is the VERY LAST step of your calculation to avoid stack overflow
- //Always use a result accumulator to remember the result of each step
- //getEven tail (head::accumulator) instead of head::getEven tail
- //Q2 modify Q1 so that you can put the reverse the result list in the function call directly
- let rec getEven2 mylis accumulator=
- let getEvenInner mylis acc=
- match mylis with
- | [] -> acc
- | head::tail when head%2=0 -> getEven2 tail (head::acc)
- | _::tail -> getEven2 tail acc
- (List.rev (getEvenInner mylis accumulator))
- let result2=getEven2 mylis []
- printf "Q2..."
- AssertEquality (result2, [2;4;6])
- //Q3 modify Q2 and create the below function which will grab the odd numbers out from a list
- let rec getOdd lis accumulator =
- let getOddinner lis acc =
- match lis with
- | [] -> accumulator
- | head::tail when head%2=1 -> getOdd tail (head::acc)
- | _::tail -> getOdd tail acc
- (List.rev(getOddinner lis accumulator))
- let result3=getOdd mylis []
- printf "Q3..."
- AssertEquality (result3, [1;3;5])
- //Q4 Get the sum of all the even numbers in a list
- let rec getEvenSum mylis sumAcc=
- match mylis with
- | [] -> sumAcc
- | head::tail when head%2=0 -> getEvenSum tail (sumAcc+head)
- | _::tail -> getEvenSum tail sumAcc
- let result4=getEvenSum mylis 0
- printf "Q4..."
- AssertEquality (result4, 12)
- //Q5 calculate the square of each number in the input list
- let rec getSqr mylis sqrLis=
- let rec getSqrInner mylis sqrLis=
- match mylis with
- | [] -> sqrLis
- | head::tail -> getSqr tail ((head*head)::sqrLis)
- (List.rev(getSqrInner mylis sqrLis))
- let result5=getSqr mylis []
- printf "Q5..."
- AssertEquality (result5, [1;4;9;16;25;36])
- //Q6 Calculate factorial of n
- //n! = 1*2*3*...*(n-1)*n
- let rec factorial n ftRes=
- match n with
- | 0 -> 1
- | 1 -> 1
- | _ -> n * factorial (n-1) 1
- let result6=factorial 5 1
- printf "Q6..."
- AssertEquality (result6, 120)
- //Q7 You can merge the 2 "if statements" of the pattern 0 and 1 together like below:
- let rec factorial1 n ftRes=
- match n with
- | 0 | 1 -> 1
- | _ -> n * factorial1 (n-1) 1
- let result7=factorial1 5 1
- printf "Q7..."
- AssertEquality (result7, 120)
- //Q8 to help user call this function easier by using "factorial 5" instead of "factorial1 5 1"
- //(so that the API user do not need to worry about giving us the accumulator as the second func input),
- //we can wrap the recursive function call inside of regular function that looks nicer
- let nicer_factorial n =
- let rec factorial1 n ftRes=
- match n with
- | 0 | 1 -> 1
- | _ -> n * factorial1 (n-1) ftRes
- factorial1 n 1
- let result8=nicer_factorial 5
- printf "Q8..."
- AssertEquality (result8, 120)
- //////////////////////////////////////////////////////////
- // queue definition stuff
- type Queue = QueueContents of int list
- let EMPTY = QueueContents []
- let enqueue x q =
- let (QueueContents contents) = q
- let newContents = List.append contents [x]
- QueueContents newContents
- let dequeue (QueueContents contents) =
- match contents with
- | head::tail ->
- let newQueue = QueueContents tail
- (contents.Head, QueueContents contents.Tail)
- | [] -> failwith "Queue is Empty"
- // create random number jobs
- let createJobs count =
- let rnd = System.Random()
- List.init count (fun _->rnd.Next(1,9))
- // parse list
- let rec workJobs jobqueue joblis=
- match joblis with
- | [] -> printfn "Reached end of available jobs"
- | head::tail when head%2=1 ->
- printf "New job %d added to Queue.\n" head
- workJobs (enqueue head jobqueue) tail
- | head::tail when head%2=0 ->
- try
- let front, newQueue = dequeue jobqueue
- printf "Completed job %d.\n" front
- workJobs newQueue tail
- with
- | Failure(msg) -> printfn "No more jobs to do right now."
- workJobs jobqueue tail
- | _::tail -> workJobs jobqueue tail
- let main =
- let jobs = createJobs 10
- printfn "%A" jobs
- workJobs EMPTY jobs
- main
- (*
- let testQ = EMPTY |> enqueue 1 |> enqueue 2
- printf "%A" testQ
- let front, testQ2 = dequeue testQ
- printf "%d, %A" front testQ2
- let testQ3 = enqueue 3 testQ
- printf "%A" testQ3
- *)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement