Question
This program needs to be written in F sharp language. Below is what the functions are suppose to do and the expected outcome. - 1)
This program needs to be written in F sharp language. Below is what the functions are suppose to do and the expected outcome. -
1) Define function to add 2-tuples
add (3, 4) (5, 6);; val it : int * int = (8, 10)
2) halve define the function third that divides an Any list into a tuple of three Any lists
third [1;2;3;1;2;3;1;2;3;1;2;3];; val it : int list * int list * int list = ([1;1;1;1], [2;2;2;2], [3;3;3;3])
3) Define the functional dup so that dup (f, x) is f (f x) where f parameter is a type and f returns that type
DupInc & Dup Tail
"
let inc (x : int) : int = x + 1; dup inc 5; Int = 7 let tail (L : 'a list) = L.Tail;; dup tail [1;2;3;4;5];; val it : int list = [3;4;5]
let f (x : 'a) : 'a list = [x];; f 4;; val it : int list = [4] f "hello";; val it : string list = ["hello"] f [1; 3];; val it : int list list = [[1; 3]]
4) Define the functional comp so that comp(f,g,x) is g(f (x)) where f parameter and result are same type, and g parameter is result type of f.
comp inc inc 5;; val it : int = 7
let above10 x = x > 10;; above10 14;; val it : bool = true comp inc inc 5;; val it : int = 7 comp inc above10 10;; val it : bool = true
5. Define a function sqlist to square every element of a given list using sq and the map functional where let sq x = x * x;;
sqlist [1;2;3;4];; val it : int list = [1;4;9;16]
Use:
let rec map f L = match L with | [] -> [] | h::t -> f h::map f t;;
6. Define a function vecadd to add two integer lists using map2 and add where let add x y = x + y;;
vecadd [1;2;3] [4;5;6];; val it : int list = [5;7;9]
Use:
let rec map2 f L1 L2 = match (L1,L2) with | ([], []) -> [] | (h1::t1, h2::t2) -> f h1 h2::map2 f t1 t2;;
7. Define a function matadd to add two integer matrices using vecadd for vector addition and map2 for recursion. matadd is a one line function.
matadd [ [1;2]; [3;4] ] [ [5;6]; [7;8] ];; val it : int list list = [ [6;8]; [10;12] ]
8. Define a function ip to compute the inner product of two lists using map2 and reduce. The inner product is the sum of the products of multiplying two lists. In the class notes we defined a function such that reduce add 0 [1;2;3] = 6 and map2 mul [1;2;3] [4;5;6] = [4;10;18]. ip is a one line function.
ip [1;2;3] [1;2;3];; val it : int = 14
Use:
let rec reduce f a L = match L with | [] -> a | h::t -> f h (reduce f a t);;
Incomplete functions in f sharp -
let addTuples a b = a let third L = ([], [], []) let thirdComplexity = NoAns let dup f x = x let comp f1 f2 x = x let sqlist L = [] let sqlistComplexity = NoAns let vecadd L1 L2 = [] let matadd L1 L2 = [] let ip L1 L2 = 0
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started