0

I have this simple function:

val CLC_seq=
       fn (n) =>
       (Cons (n, find_CLC_seq(COL_seq(n))))

When:

find_CLC_sqe is : int seq -> int;
COL_seq is: fn: int -> int seq;

The complier wrote:

Error: operator and operand don't agree
operator domain: int * (Unit -> int seq)
operand: int * int
in expression: 
 (Cons (n, find_CLC_seq(COL_seq(n))))

What is the reason? How can I solve it? Thank you.

Tom
  • 673
  • 4
  • 12
  • 20

1 Answers1

0

Well, it's not clear what you're trying to do exactly, but the compiler is right to pick you up on it. find_CLC_seq returns an int, which means your Cons is trying to cons an int onto an int. That makes no sense, because cons is for adding an element to the front of a list (your Cons function is expecting to put an int on the front of a lazy sequence, a (Unit -> int seq)).

I don't know what CLC and COL are, but it looks like either:

Your definition of CLC_seq is wrong, because if find_CLC_seq is really meant to return an int, it doesn't make sense to be using it that way;

OR your definition of find_CLC_seq is wrong, and its return type should be int seq or a lazy sequence, as the name implies. In that case, the error is in a bit of code you haven't shown us.

Nicholas Wilson
  • 9,435
  • 1
  • 41
  • 80
  • Thank you a lot, but I still don't get what is the problem. find_CLC_seq returns an int (as I wrote:find_CLC_sqe is : int seq -> int;) . so It will do Cons (int, int). Why the complier see it as Unit -> int seq? – Tom May 27 '11 at 14:38
  • Yes, your code is trying to cons an int to an int (that's what it means when it says 'operand: int * int'). The Cons constructor doesn't want those arguments; it's meant to be used to cons an int to a sequence. The type of Cons will be `int * (Unit -> int seq) -> (Unit -> int seq)`, which is what it means when it says that input for 'operator domain'. The 'operand' is what you tried to pass it, the 'operator domain' is what you should be passing. – Nicholas Wilson May 27 '11 at 14:50
  • Did you write find_CLC_seq yourself? If so, did you intend it to return an int? I can see that it does, but is that what you want? From the name of the function, it sounds like it should be returning a sequence, in which case the mistake might be there. – Nicholas Wilson May 27 '11 at 14:52
  • find_CLC_seq return an int! the compiler wrote me that "find_CLC_sqe is : int seq -> int;". find_CLC_sqe is returing the location of the first number 5 that appear in the sequence. – Tom May 27 '11 at 15:03
  • Before it, I defined: datatype 'a seq = Nil | Cons of 'a * (unit -> 'a seq);. Maybe this is the problem? Why the complier can't do cons of int*int? – Tom May 27 '11 at 15:04
  • It can't do cons of int*int because that makes no sense. Your datatype looks fine, wherever you got that from. You're going to have to explain the CLC_seq function is meant to be doing then. Could you post your complete code and the question you are trying to solve so we can see what the code is trying to solve? – Nicholas Wilson May 27 '11 at 15:08
  • last question, I promise: Why I can't do cons (int*int)? If I want pair of int*int, How I do that? – Tom May 27 '11 at 15:14
  • Cons adds an element to the head of a list, which might go on for a while. A pair of elements (a 'tuple') by definition has a fixed length, so you can't use Cons to make them. A pair of ints, an `int*int`, is simply made using `(..,..)`. If you want CLC_seq to return a pair of ints, just get rid of the word 'Cons' from the code in your question. – Nicholas Wilson May 27 '11 at 15:19