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:は、再帰的に
- 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?
ああ、それは完璧な理にかなっています。お手伝いありがとう! – XXIV
私は、スタックオーバーフローが質問内の別の質問に嫌なことがあることは知っていますが、それは簡単な質問なので問題ではないと思います。リスト[[[1]、[2]、[3]、[4]]を[[1,2]、[3,4]]に作る必要があります。 'x'が最初の値で' xs'がリストの最後の値なので、リストの次のインデックスにはどうすればアクセスできますか? – XXIV
インデックスをまったく使用しないと思わない場合は、代わりにパターンマッチングを使用してください。ヒント: '[x] :: [y] :: zsは、それぞれがシングルトンリストである少なくとも2つの要素を持つリストのパターンです。 –