Search code examples
functional-programmingsml

recursion compilation error in Standard ML


I am making a function that recurses over a string and replaces each instance of a user-specified character. However, I get a compilation error of :

Error: operator and operand do not agree [tycon mismatch]
  operator domain: string * string
  operand:         char * 'Z

I would like to know what this error means for my program and what I could be doing wrong. I'm new to SML and have been trying to research this for some time now. Thanks.

This is my code:

fun remCharR(expr, letter) = 
    if String.sub(expr, 0) = letter 
    then remCharR(String.substring(expr, 0, 1), letter)
    else String.sub(expr, 0) ^ remCharR(String.substring(expr, 0, 1), letter);

Solution

  • ^ operator needs two string operands and the first operator in String.sub(expr, 0) ^ remCharR(String.substring(expr, 0, 1), letter) is of type char. To fix this error, change String.sub(expr, 0) to String.substring(expr, 0, 1).