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);
Alejandro
  • 623
  • 1
  • 9
  • 22

2 Answers2

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

  • this is great! however, is there a reason this function is not printing when I call it? – Alejandro Feb 16 '20 at 14:03
  • 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 -- https://en.wikipedia.org/wiki/Fold_%28higher-order_function%29. – Venkatesh-Prasad Ranganath Feb 16 '20 at 16:05
  • 1
    Or `String.translate`. Since every `char` corresponds to 0 or 1 `char`s in the result, this would also fit into the functional pattern of a "concat-map", except monomorphic (pertaining only to `char`s in `string`s). – sshine Feb 16 '20 at 23:06
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
Community
  • 1
  • 1
sshine
  • 15,635
  • 1
  • 41
  • 66