How to present string operations, e.g. string replace, by math notations

I attempt to describe an algorithm in the paper, and the algorithm is about string replace operation. The algorithm is complex, so a simple example is given instead as follows:

For a given number serial, all "1" will be replaced by "a"

How to present the procedure in math notation for an abstract, and general reason?

>Solution :

I’m not sure if that’s what you were asking, but I’m assuming you want to write a program where you can assign different operations to math symbols, e.g.

Serial("1234") + 'a'

Should result in Serial("a234").

You can achieve this in Kotlin by using operator overloading. That means if you add a function to class Serial named plus and give it the replacement logic, you can "add" ‘a’ to your Serial and it will return a new serial with the ‘1’ replaced:

data class Serial(val raw: String) {
    operator fun plus(replacement: Char) : Serial {
        return Serial(raw.replace('1', replacement))
    }
}

val s = Serial("1234")
println(s + 'a')

Depending on your programming language, operator overloading is possible or not.

Leave a Reply