Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Solve this USING F#

image text in transcribed

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle 12c SQL

Authors: Joan Casteel

3rd edition

1305251032, 978-1305251038

More Books

Students also viewed these Databases questions

Question

=+Part 1 What kind of client could use vernacular in the campaign?

Answered: 1 week ago