0

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);

2 Answers 2

1

^ 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).

Sign up to request clarification or add additional context in comments.

3 Comments

this is great! however, is there a reason this function is not printing when I call it?
If the intention is to remove every instance of letter in expr, then you need to accumulate the non-letter characters, make sure String.substring(expr, 0, 1) is indeed resulting in the tail-part of the expr, and return the accumulated characters when the termination/anchor condition of the recursion is met. BTW, you could consider using foldr or foldl to accomplish this -- en.wikipedia.org/wiki/Fold_%28higher-order_function%29.
Or String.translate. Since every char corresponds to 0 or 1 chars in the result, this would also fit into the functional pattern of a "concat-map", except monomorphic (pertaining only to chars in strings).
0

I am making a function that recurses over a string and replaces each instance of a user-specified character.

A function that corresponds to this description is String.map. Example:

- String.map (fn c => if c = #"l" then #"k" else c) "hello";
> val it = "hekko" : string

Since this function can't remove characters, this suggests that "replace" is an ambiguous word.

I would like to know what this error means for my program and what I could be doing wrong.

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

Venkatesh-Prasad Ranganath already answered this and showed how to fix it.

So instead I will show an alternative way to implement this.

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);

Since the sole task of this function appears to remove characters, use String.translate:

fun remChar (c1, s) =
    String.translate (fn c2 => if c1 = c2 then "" else str c2) s

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.