coding: math operators with strings

in basic, the way to concatenate strings could have gone like this:

p$ = concat$(a$, b$)

however, someone thought it was better to overload the addition + operator:

p$ = a$ + b$

this also works in python:

p = a + b

python took this a step further… arrays can also be concatenated:

p = a + b
# [‘a’, ‘b’] + [‘c’, ‘d’] = [‘a’, ‘b’, ‘c’, ‘d’]

and multiplied:

p = a * 2
# [‘a’, ‘b’] * 2 = [‘a’, ‘b’, ‘a’, ‘b’]

as can strings:

p = a * 2
# “allo” * 2 = “alloallo”

but thats about it.

there is a split function in python that can turn a string into an array:

“suppose you have this string”

and you want to split it by a space: ” ”

the syntax for that is:
“suppose you have this string”.split(” “)

this gives you the array:

[‘suppose’, ‘you’, ‘have’, ‘this’, ‘string’]

why doesnt it make sense to use divide for that? it probably does:

“suppose you have this string” / ” ” =
[‘suppose’, ‘you’, ‘have’, ‘this’, ‘string’]

what happens if you split “hello” with “e”?

“hello” / “e” = [‘h’, ‘llo’]

this is still what the split command in python does. we are just using division as shorthand, in the tradition of + for concatenation and * for string multiplication.

python lets you multiply an array by a number, but not an array by a string. but if we use division as shorthand for split (string -> array), what about its complement, join (array -> string)?

[‘h’, ‘llo’] * “e” = “hello”

nonsense, right? well, is this easier?

“e”.join([‘h’, ‘llo’])

thats a real python expression. if we can agree that division is an appropriate shorthand for split, then multiplication is an appropriate shorthand for join.

in basic, you can add “5” to “hello” though you cant add 5 to “hello.”

its a type issue. in js, typing is weak and you can add numeric 5 to string “hello”:

“hello5”

i think its better to return an error, in case a numeric is not expected.

python is dynamically typed, but not weakly typed. it lets the same variable hold a string or a numeric, but it doesnt let you add a string to a numeric:

TypeError: cannot concatenate str and int objects

so whether this should produce “hello5” depends on the type conventions of the language, imo.

if we are dividing “hello” with “there”, then “there” does not go into “hello” — since it cant divide it, the string should not be divided:

“hello” / “there” = “hello” (this is what split does in python as well.)

due to this convention, a string should not be able to divide itself. a number divided by itself would produce “1” — we can translate “1” as “one character”

“hello” / “hello” = “h” #### is this useful?

or as “one string”:

“hello” / “hello” = “hello”

5 / 5 = 1, but also: 1 (string) / 1 (string) = 1 (string.)

should we be able to divide a string by “” ? no, python doesnt allow that, and numbers dont either. whats the string equivalent of a divide by zero error? it already exists in python:

ValueError: empty separator

but suppose we got rid of this error. we could make “hello” / “” evaluate to:

* “hello”
* “hello” or “h”
* or “”

personally i would be inclined to say that “hello” / “” returns an error, or is “hello”

what do i base that on?

languages exist to allow the expression of ideas.

so i base it on the idea: “what would the average coder expect it to do?”

…within reason.

if you multiply “pete” times 5, you get: “petepetepetepetepete” right?

i mean thats what happens when you add “pete” to “pete” 4 times:

“pete”
“petepete”
“petepetepete”
“petepetepetepete”
“petepetepetepetepete”

so if you divide “petepetepetepetepete” by 5, what should you get?

“pete” yes, thats a logical answer.

but python can step through string bytes in a “for x in string” loop the way basic steps through numbers in a “for w = x to y step z” loop:

for x in “hello”: print x #### this is a lot like dividing a string.

for whatever reason, i think this would be useful:

“petepetepetepetepete” / 5 = [‘pete’, ‘pete’, ‘pete’, ‘pete’, ‘pete’]

and if you do “mid 1, 1” (in fig) on the resulting array:

“petepetepetepetepete” / 5 : mid 1 1

you get:

“pete”

its a thought. perhaps you like “petepetepetepetepete” / 5 = “pete” better. usefulness and expectations may go together here, or work against each other.

can you divide “hello there” by 20?

no. whether you go with:

“petepetepetepetepete” / 5 = [‘pete’, ‘pete’, ‘pete’, ‘pete’, ‘pete’]
or:
“petepetepetepetepete” / 5 = [‘pete’]

dividing “hello there” by 20 gives you bytes that are less than 1.0 in size. you could try bits, but lets not.

“hello there” / 20 =
cannot divide “hello there” by 20.

“hello ” + there = “hello there” in basic.

so what does “hello there” – “there” = ?

“hello ”

what does “hello there” – “e” = ?

in my opinion, it makes the most sense for it to result in “hllo thr” though perhaps you think it should only remove a single e. which one? up to you i guess, i went with all of them.

so there you have it, hopefully practical shorthand inspired by basics + for string concatenation.

some of these could be useful if implemented. leave a comment if theres one you like or do not like.

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s