本文介绍了标准ML:操作符和操作数不同意(循环)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我试图在SML中编写一个函数来翻转列表中的交替元素。这是我的功能:

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);

当我在交互式解释器中使用我的文件(Ullman.sml)时,错误:

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

所以SML说它需要一个整数列表列表,但我只给它一对整数列表?

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.

谢谢,
bclayman

推荐答案

第二种情况是错误的;你想

Your second case is wrong; you want

fun flipAlternate(nil) = nil
 | flipAlternate([x]) = [x]
 | flipAlternate(x::y::xs) = y::x::flipAlternate(xs);

SML正在研究第二种情况并得出结论

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.

编辑:它知道结果是第一个案例中的 list ,并且结束于参数还有一个列表比第二种情况的结果。

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.

这篇关于标准ML:操作符和操作数不同意(循环)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 07:21