Waiting for answer This question has not been answered yet. You can hire a professional tutor to get the answer.

QUESTION

Use F#. In particular, solutions should not use imperative programming constructs, or predefined (built-in) functions that make the solutions...

Use F#. In particular, solutions should not use imperative programming constructs, or predefined (built-in) functions that make the solutions completely trivial.

For each problem, write a single function definition without any nested functions. Your solutions do not need to be tail recursive.

Define a function called max that takes a list and returns the largest value in the list. You may assume that the list is not empty. Do not use the built-in List.max function.

Define a function called nth that takes an integer, n, and a list as arguments. The nth element of the list, indexed from 0, should be returned. You may assume that the list has at least one element.

Define a function called zip that takes a pair (tuple) of equal length lists as a single parameter and returns a list of pairs. The first pair should contain the first element of each list, the second pair contains the second element of each list, and so on.

Define a function called search that takes a boolean function, f, and a list as arguments and returns the zero-based index of the first element of the list, e, such that (f e) is true. If no such element exists, search should return -1.

The following examples show sample solution.

> max [89; 42; 112; 36; 90; -205]

val it : int = 112

> max [-5; 0; -12; -3]

val it : int = 0

> max ["c"; "a"; "p"; "f"]

val it : string = "p"

> nth 2 [0; 1; 2; 3; 4]

val it : int = 2

> nth 3 ["cat"; "dog"; "bird"; "fox"]

val it : string = "fox"

> nth 1 ["cat"; "dog"; "bird"; "fox"]

val it : string = "dog"

> zip ([1], [2])

val it : (int * int) list = [(1, 2)]

> zip (["a"; "b"; "c"; "d"; "e"], [1; 2; 3; 4; 5])

val it : (string * int) list = [("a", 1); ("b", 2); ("c", 3); ("d", 4); ("e", 5)]

> search (fun x -> x > 10) [ 2; 12; 3; 23; 62; 8; 2 ]

val it : int = 1

> search (fun x -> x%2 = 0) [ 3; 17; 9; 23; 62; 8; 2 ]

val it : int = 4

> search (fun s -> s val it : int = 3

> search (fun n -> n = 5) [ 3; 7; 12; 6 ]

val it : int = -1

Show more
LEARN MORE EFFECTIVELY AND GET BETTER GRADES!
Ask a Question