Fill This Form To Receive Instant Help
Homework answers / question archive / OCAML Programming Q4 OCaml 15 Points Q4
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?
Already member? Sign In