I’m trying to make a standard ml function which takes 3 elements as input and returns a sorted list which is sorted from smallest to largest. I used 3 helper methods that gets me the min, max and the mid elements. the codes are below:
- fun min3 (a, b, c):real =
if a < b andalso a < c then a
else if b < a andalso b < c then b
else c;
- fun mid3 (a, b, c):real =
if (a < b andalso a > c) orelse (a > b andalso a < c) then a
else if (b < a andalso b > c) orelse (b > a andalso b < c) then b
else c;
- fun max3 (a, b, c):real =
if a > b andalso a > c then a
else if b > a andalso b > c then b
else c;
- fun sort3 (a, b, c):real =
min3(a, b, c)::mid3(a, b, c)::max3(a, b, c)::[];
the following worked perfectly when dealing with ints, but when i changed them to reals, the helper methods returned the correct results but i get error when typing the sort method which is the following (couldnt copy the error text so i took a screen shot) :

what could be wrong in the code?
Thanks
Also, is there another way of sorting 3 elements other than the way i posted here or not?
When changing the types, you’ve made a mistake with the return value of
sort3. The error message is telling you that you declaredsort3to return a real while in fact it returns a list of reals.