Standard ML: Operator and Operand Don't Agree (Circularity)

大兔子大兔子 提交于 2020-01-05 08:29:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!