Trusted by Students Everywhere
Why Choose Us?
0% AI Guarantee

Human-written only.

24/7 Support

Anytime, anywhere.

Plagiarism Free

100% Original.

Expert Tutors

Masters & PhDs.

100% Confidential

Your privacy matters.

On-Time Delivery

Never miss a deadline.

OCAML Programming Q4 OCaml 15 Points Q4

Computer Science Dec 17, 2020

OCAML Programming

Q4 OCaml

15 Points

Q4.1 Write Function of Type

3 Points

Write a function of the type 'a list -> 'b list -> ('a * 'b * 'b). The function must not contain any non-exhaustive pattern matching, but you are allowed to raise exceptions.

Q4.2 Recursive `count`

3 Points

Write a function called count that takes in a value and a list and returns the number of times the value occurs in the list (as determined by =). You may not use functions from the List module, and you may not use map or fold. The function can be recursive. You may not define any helper functions.

For example:

count 1 [1; 1; 3; 2; 1] = 3

count 'a' ['b'; 'a'; 'a'; 'c'] = 2

count 5 [] = 0

The function header should be let rec count x lst = (include this in your answer)

Q4.3 Non-recursive `count`

3 Points

Now re-write the function count from the previous question so that it uses List.fold_left. This function must not be recursive. You may not use any functions from the List module. You may not define any helper functions.

You may use map or fold, given below:

let rec map f l =

    match l with

    | [] -> []

    | h :: t -> (f h) :: (map f t)

 

let rec fold f acc l =

    match l with

    | [] -> acc

    | h :: t -> fold f (f acc h) t

The function header should be let count x lst = (include this in your answer)

Q4.4 Total Length of list list

6 Points

Write a function called total_size which takes in a 'a list list and returns the total number of elements in all of the sublists. You may not define helper functions, but you can use map or fold which are given above. It is up to you whether to make the function recursive.

For example:

total_size [] = 0

total_size [[]; []] = 0

total_size [[1]; [2]; [3; 5]] = 4

total_size [[1] [2; 10; 2]; []; [3; 5]; []] = 6

The function header should be let rec total_size lst = (include this in your answer, rec is optional)

 

 

Q2.2 Functional Paradigms

2 Points

The functional programming paradigm prefers immutable values.

True

False

Q2.3 Static vs Dynamic Typing

4 Points

What is one benefit of using a statically-typed language?

What is one benefit of using a dynamically-typed language?

 

Archived Solution
Unlocked Solution

You have full access to this solution. To save a copy with all formatting and attachments, use the button below.

Already a member? Sign In
Important Note: This solution is from our archive and has been purchased by others. Submitting it as-is may trigger plagiarism detection. Use it for reference only.

For ready-to-submit work, please order a fresh solution below.

Or get 100% fresh solution
Get Custom Quote
Secure Payment