Question: 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

IN ML: Define mylist type that is the same as the one

?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 mylisti

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!