Answered step by step
Verified Expert Solution
Question
1 Approved Answer
IN ML: Define mylist type that is the same as the one on page 172 of the textbook ?NTCONS INTCONS INTNIL the empty list INTCONS
IN ML:
Define mylist type that is the same as the one on page 172 of the textbook
?NTCONS INTCONS INTNIL the empty list INTCONS 1 CINTNIL the list [1,2 the list [1] This ML session constructs the values pictured above: INTNIL val it = 1NTNIL : ?ntlist -INTCONS (1, INTNIL) val itINTCONS (1, INTNIL) intlist INTCONS (1, NTCONS (2, INTNL) ) ; val it INTCONS (1, INTCONS (2, , INTNIL)) int Of course, ML does not display those values in the standard list would have to write your own function to do that. The function intl below, computes the length of an intlist. Compare this with the function you the length of an ordinary int list using the same method. See how similar the two types are? fun intlistLength INTNTL = 0 l intlistLength (INTCONS (,tail)) - 1 (intListLength tail) fun listLength nil 0 | listLength (::tail) = - 1 (listLength tail) Of course, there is one major difference between the predefined list typecn structor and intlist: the predefined 1ist is parametric, while intlist works only for lists of integers. This is easily remedied. The following datatype, mylist. is just like intlist, but parametric. Compare the two definitions and you wll st that the only change was to replace the int type with a type va parameter for the type constructor variable, added as a datatype 'element mylist NIL CONS of 'element 'element mylisti ?NTCONS INTCONS INTNIL the empty list INTCONS 1 CINTNIL the list [1,2 the list [1] This ML session constructs the values pictured above: INTNIL val it = 1NTNIL : ?ntlist -INTCONS (1, INTNIL) val itINTCONS (1, INTNIL) intlist INTCONS (1, NTCONS (2, INTNL) ) ; val it INTCONS (1, INTCONS (2, , INTNIL)) int Of course, ML does not display those values in the standard list would have to write your own function to do that. The function intl below, computes the length of an intlist. Compare this with the function you the length of an ordinary int list using the same method. See how similar the two types are? fun intlistLength INTNTL = 0 l intlistLength (INTCONS (,tail)) - 1 (intListLength tail) fun listLength nil 0 | listLength (::tail) = - 1 (listLength tail) Of course, there is one major difference between the predefined list typecn structor and intlist: the predefined 1ist is parametric, while intlist works only for lists of integers. This is easily remedied. The following datatype, mylist. is just like intlist, but parametric. Compare the two definitions and you wll st that the only change was to replace the int type with a type va parameter for the type constructor variable, added as a datatype 'element mylist NIL CONS of 'element 'element mylistiStep 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