programming languages - Differentiate between atom and list in SML -
I'm new to SML programming and I have a problem creating a function to delete an Atom A in the list. . This list can be nested at any level, it means that we can make lists like [1,2,3] and we have a list such as [[1,2], [2,3]] and Also [[[1] list, 2], [1,2]], [[2,3], [2,3]].
So my problem is how can I check that the given item is a list or an atom as I have no such function in SMLNJ till date?
I have created a function that checks whether the list is empty or not, and then it calls for auxiliary assistant to check whether the main list is a list or an atom or not. If it is an atom then replace it with another atom and continue with the rest of the tail.
If I see that the tail of the tail list is empty inside the assistant check, then it gives an error as the tail function can only be a list.
I want to do it like tl ([hd (a))
, and if I do this, it will always be empty.
If I apply it to the first list, I get the head as 1 and wrap it in [] [1], so its tail will be []. In the same way if I get the head of the second list then it will [1,2] and wrapping it in [] will result in [[1,2]], this type of tail will be again []. So is there any way that I can check whether the given object is an atom or a list?
Thanks in advance for all the responses.
"This list may be nested at any level" is not possible in SML, as it is stable Type, and a specific type of type of type is a type you have either a int list
, which is a list whose element is all the int
, or int list list
, which is a list whose elements are all int list
. You can not have any mix
You must create an algebraic datatype with the nested list of the things that you are talking about, the closest to it, the two cases, the one leaf or the elements of this datatype. You can then use pattern matching to decode this datatype.
Comments
Post a Comment