Question: Solve this USING F# Consider the following datatype: type fp = | Nil | IntCons of int * fp | StrCons of string * fp

Solve this USING F#

Solve this USING F# Consider the following datatype: type fp = |

Consider the following datatype: type fp = | Nil | IntCons of int * fp | StrCons of string * fp (i) Write a function from IntList : int list -> fp that takes a list of integers and converts it into an element of fp by replacing :: with IntCons. > fromIntList [] ;; val it : fp = Nil > from IntList (1;3; 9];; val it : fp = IntCons (1,IntCons (3,IntCons (9, Nil))) (ii) Write a function extract Ints : fp -> int list that extracts the integers from the given element of fp. > extract Ints (from IntList [1..5]);; val it : int list = [1; 2; 3; 4; 5] > extract Ints (IntCons (1, StrCons ("x", from IntList (5. .7]))) ;; val it : int list = [1; 5; 6; 7] > extract Ints (StrCons ("x", Nil));; val it : int list = [] (iii) Write a function valid : fp -> bool that returns true when there are no two adjacent ints in the argument, and no two adjacent strings. > valid Nil;; val it : bool = true > valid (StrCons("x", Nil));; val it : bool = true > valid (IntCons(1, StrCons("x", IntCons(2, StrCons("y", Nil))))) ;; val it : bool = true > valid (StrCons ("x", IntCons(1, IntCons (2, Nil)))) ;; val it : bool = false > valid (StrCons("x", StrCons("y", Nil)));; val it : bool = false (iv) Write a function norm : fp -> fp that sums adjacent ints and concatenates adjacent strings in an fp, so that valid (norm 1) = true for all 1. > norm Nil;; val it : fp = Nil > norm (IntCons(1, StrCons ("x", IntCons (3, StrCons("y", Nil))))) val it : fp = IntCons (1, StrCons ("x", IntCons (3,StrCons ("y",Nil)))) > norm (from IntList [1.. 10]) ;; val it : fp = IntCons (55, Nil) > norm (IntCons(1, StrCons ("Hello, ", StrCons ("World!", IntCons (2, Nil)))));; val it : fp = IntCons (1, StrCons ("Hello, World!", IntCons (2,Nil))) Consider the following datatype: type fp = | Nil | IntCons of int * fp | StrCons of string * fp (i) Write a function from IntList : int list -> fp that takes a list of integers and converts it into an element of fp by replacing :: with IntCons. > fromIntList [] ;; val it : fp = Nil > from IntList (1;3; 9];; val it : fp = IntCons (1,IntCons (3,IntCons (9, Nil))) (ii) Write a function extract Ints : fp -> int list that extracts the integers from the given element of fp. > extract Ints (from IntList [1..5]);; val it : int list = [1; 2; 3; 4; 5] > extract Ints (IntCons (1, StrCons ("x", from IntList (5. .7]))) ;; val it : int list = [1; 5; 6; 7] > extract Ints (StrCons ("x", Nil));; val it : int list = [] (iii) Write a function valid : fp -> bool that returns true when there are no two adjacent ints in the argument, and no two adjacent strings. > valid Nil;; val it : bool = true > valid (StrCons("x", Nil));; val it : bool = true > valid (IntCons(1, StrCons("x", IntCons(2, StrCons("y", Nil))))) ;; val it : bool = true > valid (StrCons ("x", IntCons(1, IntCons (2, Nil)))) ;; val it : bool = false > valid (StrCons("x", StrCons("y", Nil)));; val it : bool = false (iv) Write a function norm : fp -> fp that sums adjacent ints and concatenates adjacent strings in an fp, so that valid (norm 1) = true for all 1. > norm Nil;; val it : fp = Nil > norm (IntCons(1, StrCons ("x", IntCons (3, StrCons("y", Nil))))) val it : fp = IntCons (1, StrCons ("x", IntCons (3,StrCons ("y",Nil)))) > norm (from IntList [1.. 10]) ;; val it : fp = IntCons (55, Nil) > norm (IntCons(1, StrCons ("Hello, ", StrCons ("World!", IntCons (2, Nil)))));; val it : fp = IntCons (1, StrCons ("Hello, World!", IntCons (2,Nil)))

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!