Search code examples
sml

SML operator and operand do not agree


I am trying to write my first function in sml. It takes a tuple and returns the sum of first element times 10, second element times 6 and the third, and then divides by 10. I don't know what I am doing wrong here I get this error operator and operand do not agree [tycon mismatch].

fun rgb2gray(rgb: (int*int*int))=
let
    val x = (#1rgb * 3 )+ (#2rgb * 6 )+ (#3rgb)
in  
    x=x/10
end

Solution

  • x=x/10 is an equality comparison (and will only be true if x is zero), and / is for dividing reals, not integers.
    (+, -, and * are overloaded, but / isn't.)

    Integer division is called div, and since the value of the function should be x div 10, you only need to write x div 10, without any =.

    It's more common to use pattern matching than selectors for deconstructing structures, and I would write your function like this:

    fun rgb2gray (r, g, b) = (r * 3 + g * 6 + b) div 10