问题
I'm trying to write a function in SML to flip alternate elements of a list. Here's my function:
fun flipAlternate(nil) = nil
| flipAlternate([x]) = x
| flipAlternate(x::y::xs) = y::x::flipAlternate(xs);
When I go to use my file (Ullman.sml) in the interactive interpreter, I get this compilation error:
- use "Ullman.sml";
[opening Ullman.sml]
Ullman.sml:5.31-5.54 Error: operator and operand don't agree [circularity]
operator domain: 'Z list * 'Z list list
operand: 'Z list * 'Z list
in expression:
x :: flipAlternate xs
So SML is saying it requires a list of lists of integers but I'm only giving it a pair of lists of integers?
I'm a bit lost here so any help would be much appreciated.
Thanks, bclayman
回答1:
Your second case is wrong; you want
fun flipAlternate(nil) = nil
| flipAlternate([x]) = [x]
| flipAlternate(x::y::xs) = y::x::flipAlternate(xs);
SML is looking at the second case and concluding
flipAlternate :: 'z list list -> 'z list
which isn't compatible with the recursion in your third case.
Edit: It knows the result is a list
from the first case, and concludes the argument has one more list
than the result from the second case.
来源:https://stackoverflow.com/questions/31568786/standard-ml-operator-and-operand-dont-agree-circularity