I’m trying to make a function in standard ml that takes a list of pairs of ints, and I should return a list of int that contains the max element from the pairs. (int * int) list -> int list. Now so far,I’ve written a code but it doesnt work and I cant seem to figure the problem from the error it gave.
Here’s the code:
- fun maxpairs x =
= foldr (fn (a, b) => if a > b then a else b) [] x;
And here’s the error I’m getting:
stdIn:15.2-15.50 Error: operator and operand don't agree [overload]
operator domain: 'Z
operand: 'Y list
in expression:
(foldr (fn (<pat>,<pat>) => if <exp> then <exp> else <exp>)) nil
foldrtakes a function of type('a * 'b) -> 'b, a value of type'band a list of type['a]. In your case the list is a list of pairs and the value of type'bis an empty list. That means in the functionfn (a,b) => ...awill be a pair andbwill be a list. You then try to compareaandbusing>. Since>can’t be used with a pair as its left operand and a list as its right operand, that doesn’t work. Also you can’t have an if-statement where the then-expression and the else-expression have different types.If I were you I’d use map for this, which seems to fit the problem better than using a fold.