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는 최소한 두 개의 엘리먼트를 가진리스트의 패턴이다. 각각의 엘리먼트는 싱글 톤리스트이다. –