24

How do you read an expression like?

x + y /. x -> 2

Looking up /. and -> in Mathematica docs it says ReplaceAll and Rule. But you would not pronounce the expression above as

x plus y replace all x rule 2. 

Instead you would say something like

x plus y where x goes to two. 

The operators /. and -> are just two examples, there are a lot of others in Mathematica. Is there some resource that addresses pronunciations in Mathematica.

Carl Lange
  • 13,065
  • 1
  • 36
  • 70
Bob Ueland
  • 1,059
  • 6
  • 16
  • @C.E Of course there must be several ways to pronounce an expression, depending on the context. I am not after one true way to pronounce, but more a list of examples how you could pronounce some not obvious expressions. Maybe no one thinks this is worthwhile and that's OK, but if anybody has discussed this I would like a link to it. – Bob Ueland Apr 27 '19 at 09:14
  • This is a topic in any programming language, as they have not been designed to become acoustic streams. Are you primarily interested in communicating vocally to other people, or are you interested in clarifying your inner monologue while working on code? Some of us are more visual-oriented and a symbol like -> never makes it beyond the visual stage, never becomes an acoustic event. – Roman Apr 27 '19 at 09:16
  • @Roman I am primarily interested in communicating vocally to other people. When it comes to other programming languages there is some discussion, see for instance https://stackoverflow.com/questions/7746894/are-there-pronounceable-names-for-common-haskell-operators/16801782#16801782 – Bob Ueland Apr 27 '19 at 09:23
  • I know that pronunciations are opinion based and that there are no true answers, but still it makes sense to me that have some way of communication vocally to other people when the need arises, so the discussion in Haskell was of great help to me, even though it is opinion based. – Bob Ueland Apr 27 '19 at 09:31
  • 1
    How about you get a list started, like the Haskell example, and everybody chips in? Something like a table with three columns: "symbol", "pronounciations" (many lines), and "voters" where everybody can put in their name to vote for a particular pronounciation. – Roman Apr 27 '19 at 10:13
  • 1
    Interesting question. I am not in the habit of reading out code this way. I think in this case I would say: "in x plus y, replace all x with two" – does that seem intelligible to you? – Mr.Wizard Apr 27 '19 at 10:44
  • 1
    Clearly, the 'right' way to say it is Speak@HoldForm[x + y /. x -> 2]. I find it interesting that Speak translates -> well but has no understanding of what /. is. – bobthechemist Apr 27 '19 at 11:15
  • Haha @bobthechemist it looks like this topic hasn't gotten the deep attention at WR that other topics get! Speak@HoldForm[x -> 2] is "x goes to 2", but Speak@HoldForm[x :> 2] is "x colon goes to 2" instead of something more human like "x will go to 2". I think a collection of everybody's ideas would be a good thing to assemble. – Roman Apr 27 '19 at 12:49
  • `In[127]:= SpokenString[Hold[x + y /. x -> 2]]

    Out[127]= "Hold of the quantity x plus y slash dot x goes to 2"` Gould be a hair better on that "slash dot".

    – Daniel Lichtblau Apr 27 '19 at 13:01
  • 3
    Why is this question closed? There are already two good answers! – bill s Jul 01 '19 at 17:13
  • I agree, I would like to see this question re-opened. I think there is enough commonality for answers to not necessarily be "entirely opinion based". – Ronald Monson Jul 04 '19 at 00:04

2 Answers2

39

Starting a brain dump of ideas, listening to my inner monologue. Please feel free to edit and add suggestions. Here is a list of most operators.

| sym  | example     | pronunciation                       | votes, comments, rants
|––––––|–––––––––––––|–––––––––––––––––––––––––––––––––––––|–––––––––––––––––––––––––
| ::   | x::y        | x says y                            |
| #    | #           | slot                                |
|      | #3          | slot 3                              |
| ##   | ##          | all slots                           |
|      | ##3         | all slots from third                |
| &    | x&          | x done                              |
|      |             | x end-of-function                   |
| %    | %           | above                               |
|      |             | answer                              |
|      |             | previous                            |
| :    | x:_         | x-pattern                           |
|      |             | anything, call it x                 |
| /;   | x_/;y       | x-pattern that y                    |
|      |             | anything that y, call it x          |
| ?    | x_?yQ       | x-pattern that is y                 |
|      |             | anything y, call it x               |
| _    | x_          | x-pattern                           |
|      |             | anything, call it x                 |
| _    | x_y         | x-pattern of type y                 |
|      |             | anything of type y, call it x       |
| __   | x__         | x-patterns                          |
|      |             | any sequence, call it x             |
| ___  | x___        | x-maybepatterns                     |
|      |             | any sequence, even empty, call it x |
| _.   | x_.         | x-defaultpattern                    |
|      |             | anything, call it x, with default   |
| :    | x_:y        | x-pattern defaults to y             |
|      |             | anything, call it x, with default y |
| ..   | x..         | one or more x                       |
|      |             | x-more                              |
| ...  | x...        | zero or more x                      |
|      |             | x-maybemore                         |
| {}   | {1,2,3}     | list of 1, 2, 3                     |
| [[]] | x[[i]]      | element i of x                      |
| ;;   | x;;y;;z     | from x to y in steps of z           |
| ==   | x==y        | x equal to y                        |
| !=   | x!=y        | x not equal to y                    |
| ===  | x===y       | x same as y                         |
| =!=  | x=!=y       | x not same as y                     |
| ++   | x++         | x and then increment it             |
|      |             | x-before-increment                  |
| ++   | ++x         | x but increment it first            |
|      |             | x-after-increment                   |
| --   | x--         | x and then decrement it             |
|      |             | x-before-decrement                  |
| --   | --x         | x but decrement it first            |
|      |             | x-after-decrement                   |
| []   | f[x]        | f of x                              |
|      | f[x,y]      | f of x and y                        |
| @*   | x@*y        | y then x (read from right to left)  |
|      |             | x of y                              |
| //   | x//y        | x then y                            |
| /*   | x/*y        | x then y                            |
| @    | f@x         | f of x                              |
| ~    | x~f~y       | f of x and y                        |
|      |             | x with/using f on y                 |
| /@   | f/@x        | f mapped on x                       |
|      |             | f of all in x                       |
| //@  | f//@x       | f map-alled on x                    |
|      |             | f of everything in x                |
|      |             | f mapped on everything in x         |
| @@   | f@@x        | f-head on x                         |
|      |             | f applied to x                      |
| @@@  | f@@@x       | f-head mapped on x                  |
|      |             | f applied to all in x               |
| ->   | x->y        | x becomes y                         |
|      |             | x goes to y                         |
| :>   | x:>y        | x will become y                     |
|      |             | x will go to y                      |
| /.   | x/.y        | x where y                           |
| //.  | x//.y       | x where repeatedly y                |
| =    | x=y         | x is y                              |
| :=   | x:=y        | x will be y                         |
| ^=   | x[y]^=z     | y remembers x[y] is z               |
| ^:=  | x[y]^:=z    | y remembers x[y] will be z          |
| /:   | x/:y=z      | x remembers y is z                  |
| /:   | x/:y:=z     | x remembers y will be z             |
| .    | x=.         | x is cleared                        |
|––––––|–––––––––––––|–––––––––––––––––––––––––––––––––––––|–––––––––––––––––––
Roman
  • 47,322
  • 2
  • 55
  • 121
  • thank you for a nice beginning. I often pronounce x_ as "anything, call it x". I guess you could pronounce x__ as "any sequence, call it x". And maybe you could pronounce x___ as "any sequence including empty, call it x". – Bob Ueland Apr 27 '19 at 16:52
  • 2
    As I spend a fair amount of time teaching Mathematica to students who have never done programming, having a list of terms to use for the language's typography - if only to be self-consistent - is very helpful. – bobthechemist Apr 27 '19 at 17:25
  • @bobthechemist please feel free to add the way you would pronounce things to the list! I teach Mathematica too and would be keen to have a consistent list. – Roman Apr 27 '19 at 18:34
  • Do you have _h in your table? It represents any expression having the h as its head. For instance f[x_Integer] :=... defines a function that takes any integer as a parameter. I would pronounce x_Integer as "any integer call it x". – Bob Ueland Apr 27 '19 at 19:39
  • Yes @BobUeland I had extrapolated your earlier comment to include x_y as "anything of type y, call it x". – Roman Apr 27 '19 at 21:37
  • I think until a universal convention manages to emerge, there will be differences of personal taste. Personally, I prefer to keep the names I use close to the command names, e.g. read x /. y as "x replace-all y" or "x ReplaceAll y". Less cognitive load, reinforces learning the names of the functions. Doesn't work for syntax that are not commands, e.g. x // y for which your "x then y" is good. I'm sure if I wrote out my verbals habits, some folks would dislike some of my suggestions. :) – Michael E2 Mar 15 '21 at 16:00
  • @MichaelE2 please free to add yours, let's make an inclusive list of alternatives! – Roman Mar 15 '21 at 16:24
27

You could try using SpokenString:

SpokenString[HoldForm[x + y /. x->2]]

"x plus y slash dot x goes to 2"

Addendum

If you don't mind messing with internal functions, it is possible to customize the output of SpokenString. For this purpose, it is convenient to make use of my ExtractDownValues function reproduced below:

SetAttributes[ExtractDownValues, HoldAll]

ExtractDownValues[sym_[args__]] := ExtractDownValues[sym,args]
ExtractDownValues[sym_,args__] := Cases[
    DownValues[sym],_[h_,_] /; MatchQ[Unevaluated[sym[args]],h]
]

The internal function that does most of the heavy lifting is SpokenStringDump`SSText:

TracePrint[SpokenString[HoldForm[x + y /. x -> 2]], _SpokenStringDump`SSText]

SpokenStringDump`SSText[x+y/. x->2]

SpokenStringDump`SSText[x+y]

SpokenStringDump`SSText[x]

SpokenStringDump`SSText[y]

SpokenStringDump`SSText[2]

SpokenStringDump`SSText[x->2]

SpokenStringDump`SSText[x]

SpokenStringDump`SSText[2]

"x plus y slash dot x goes to 2"

Using ExtractDownValues we find:

ExtractDownValues @ SpokenStringDump`SSText[x + y /. x -> 2]

{HoldPattern[ SpokenStringDump`SSText[SpokenStringDump`a_ /. SpokenStringDump`b_]] :> SpokenStringDump`SpeakQuantity[SpokenStringDump`a, ReplaceAll, True] ~~ " slash dot " ~~ If[ListQ[Unevaluated[SpokenStringDump`b]], SpokenStringDump`SpeakListContents[SpokenStringDump`b], SpokenStringDump`SpeakQuantity[SpokenStringDump`b, ReplaceAll, True]], HoldPattern[ SpokenStringDump`SSText[SpokenStringDump`f_[SpokenStringDump`a___]]] :> Module[{SpokenStringDump`ans = SpokenStringDump`SSText0[SpokenStringDump`f[SpokenStringDump`a]]}, If[Head[SpokenStringDump`ans] =!= SpokenStringDump`SSText0 && ! MemberQ[SpokenStringDump`ans, $Failed, {0, 1}], SpokenStringDump`ans, SpokenStringDump`SSTextFallThrough[ Unevaluated[SpokenStringDump`f[SpokenStringDump`a]]]]]}

Notice the " slash dot " string. So, modifying this downvalue to:

SpokenStringDump`SSText[a_ /.  b_] := SpokenStringDump`SpeakQuantity[a,ReplaceAll,True] ~~ 
    " where " ~~ 
    If[ListQ[Unevaluated[b]],
        SpokenStringDump`SpeakListContents[b],
        SpokenStringDump`SpeakQuantity[b,ReplaceAll,True]
    ]

will produce:

SpokenString[HoldForm[x + y /. x->2]]

"x plus y where x goes to 2"

Carl Woll
  • 130,679
  • 6
  • 243
  • 355