I'm brand new to SML/NJ and I'm trying to make a recursive function that makes a listOfLists. Ex:
listOf([1,2,3,4])
will output[[1],[2],[3],[4]]
. I've found a recursivemerge
in SML/NJ, and I'm trying to use it as kind've an outline:Comment faire une liste récursive des listes SML/NJ
- fun merge(xs,nil) = xs
= | merge(nil,ys) = ys
= | merge(x::xs, y::ys) =
= if (x < y) then x::merge(xs, y::ys) else y::merge(x::xs,ys);
- fun listOf(xs) = xs
= | listOf(x::xs) = [x]::listOf(xs);
I'm trying to use pattern match and I'm a little confused on it. I'm pretty sure x is the head and then xs is the tail, but I could be wrong. So what I'm trying to do is use the head of the list, make it a list, and then add it to the rest of the list. But when trying to do this function, I get the error:
stdIn:15.19-15.34 Error: operator and operand don't agree [circularity]
operator domain: 'Z list * 'Z list list
operand: 'Z list * 'Z list
in expression:
(x :: nil) :: listOf xs
This error is foreign to me because I don't have really any experience with sml/nj. How can I fix my listOf function?
Ah, qui fait sens. Merci pour l'aide! – XXIV
Je sais que débordement de pile peut être difficile à propos de poser une autre question dans une question, mais comme c'est une question simple, j'espère que cela n'aura pas d'importance. Je dois faire une liste comme: '[[1], [2], [3], [4]]' dans [[1,2], [3,4]] '. Comment accéder à l'index suivant de la liste puisque 'x' est la première valeur et' xs' est la dernière valeur d'une liste? – XXIV
Ne pensez pas du tout à utiliser des indices, utilisez plutôt la correspondance de modèles. Astuce: '[x] :: [y] :: zs' est un modèle d'une liste avec au moins deux éléments, chacun étant des listes singleton. –