How can I define a heteregeneous list datatype?

て烟熏妆下的殇ゞ 提交于 2019-12-10 11:55:57

问题


I am just starting to learn SML and having issues. I want to define a datatype, for a list that is not homogeneous.
Take for example

val a = [1,[2,4,3],5,[2,6]] 

I have made this datatype

datatype 'a MulList = List of 'a multiList list
                    | E of 'a;

but I get the following error

/tmp/emacs-region29207RwC:8.34-8.43 Error: unbound type constructor: multiList

uncaught exception Error
  raised at: ../compiler/TopLevel/interact/evalloop.sml:66.19-66.27
             ../compiler/TopLevel/interact/evalloop.sml:44.55
             ../compiler/TopLevel/interact/evalloop.sml:296.17-296.20

回答1:


Your datatype declarations has a few errors. First off it is good practice (as you do) to capitalise the first letter of the value constructors, however it is also good practice to have a type constructor in all lower case.
The real error you have is that you reference your mullist type as multiList when you define the value constructor List.

With that fixed, you can create your multilist:

datatype 'a mullist = List of 'a mullist list
                    | E of 'a

val a = [E 1, List [E 2, E 4, E 3], E 5, List [E 2, E 6]]


来源:https://stackoverflow.com/questions/15787639/how-can-i-define-a-heteregeneous-list-datatype

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!