Reputation: 713
Hi i am getting compilation errors in the following SML code, can someone help?
Error: operator and operand don't agree [UBOUND match]
operator domain: 'Z list
operand: ''list
in expression:
null mylist
stdIn:4.15-4.24 Error: operator and operand don't agree [UBOUND match]
operator domain: 'Z list
operand: ''list
in expression:
hd mylist
stdIn:6.19-6.36 Error: operator and operand don't agree [UBOUND match]
operator domain: 'Z list
operand: ''list
in expression:
tl mylist
stdIn:6.10-7.21 Error: operator is not a function [circularity]
operator: 'Z
in expression: (exists_in (item,tl mylist)) exists_in
code:
fun exists_in (item: ''int, mylist:''list) =
if null mylist
then false
else if (hd mylist) = item
then true
else exists_in(item, tl mylist)
exists_in(1,[1,2,3]);
Upvotes: 0
Views: 379
Reputation: 251
What each of the error messages is telling you is that you're applying a function to something of the wrong type. For example, null
is of type 'a list -> bool
, and so expects to be applied to an argument of type 'a list
. In exists_in
, you're applying null
to something of type ''list
on line 4.
SML also provides type inference, so there's no real need to specify the types of your arguments (though it can be useful for debugging). If you do want to specify the types, as molbdnilo commented, the types you are looking for are int
and int list
, or ''a
and ''a list
('' a
being a type variable for equality types).
Unrelatedly, perhaps a more idiomatic way to write your function is do define it by case analysis on the structure of your list, rather than using boolean checks. The advantage of this is that you are immediately given the data supporting the boolean check on whether or not the list is empty, rather than checking and then extracting the data. For example:
fun exists_in (item, []) = false
| exists_in (item, h::t) = (item = h) orelse exists_in (item, t)
Upvotes: 4