From 73dd1026f4a1c16f1b00006cf710c23ed816a511 Mon Sep 17 00:00:00 2001 From: Christopher Sumnicht Date: Sat, 27 May 2017 18:39:56 -0700 Subject: [PATCH] Update details.rst Removed words from details.rst. Also, made a couple small structural changes. --- docs/details.rst | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/docs/details.rst b/docs/details.rst index 6428ef7..7b5ba38 100644 --- a/docs/details.rst +++ b/docs/details.rst @@ -1,12 +1,12 @@ 2. More Details About Pyth ************************** -Since you now have the programming environment set up to program in, let's learn some more about the language. Let's try adding some numbers now. +Now that you have the programming environment set up, let's learn some more about the language. 2.1. Pyth Uses Prefix Notation ============================== -Ok, ``2+2`` seems easy enough. Check and see what the interpreter makes of that. It'll be 4 right?:: +Try entering ``2+2``. It'll be 4 right?:: 2 Traceback (most recent call last): @@ -14,7 +14,7 @@ Ok, ``2+2`` seems easy enough. Check and see what the interpreter makes of that. File "", line 5, in TypeError: plus() missing 1 required positional argument: 'b' -Oh noes! An error! What went wrong? Well, Pyth doesn't use Infix notation like most languages do (the operator goes between the operands), but instead uses `Prefix (aka Polish) `_ notation, which means the operator goes *before* the operands. This has the benefit of not requiring parenthesis, making programs shorter and freeing up operators for other use. So let's try that math problem again:: +Oh noes! An error! What went wrong? Well, Pyth doesn't use Infix notation (the operator goes between the operands) like most languages do. Instead, Pyth uses `Prefix (aka Polish) `_ notation, which means the operator goes *before* the operands. This has the benefit of not requiring parenthesis, making programs shorter and freeing up operators for other uses. Let's try that math problem again:: +2 2 @@ -22,25 +22,27 @@ Oh noes! An error! What went wrong? Well, Pyth doesn't use Infix notation like m 4 -There, now its working like we expected it to. Notice the space between the ``2``'s so the parser doesn't interpret them as a ``22``. +Now it is working as expected! Notice the space between the ``2``'s so the parser doesn't interpret them as a ``22``. 2.2. Pyth Has Many, Many Operators ================================== -The addition operator we just saw doesn't even begin to scratch the surface of Pyth's rich variety of operators. Besides addition Pyth has all the customary arithmetic operators - ``-`` for subtraction, ``*`` for multiplication, ``/`` for division, ``%`` for modulo, and ``^`` for exponentiation. Integers are defined as you would expect and Floats with the decimal point. Ex:: +The addition operator we just saw doesn't even begin to scratch the surface of Pyth's rich variety of operators. As well as addition, Pyth has all the customary arithmetic operators - ``-`` for subtraction, ``*`` for multiplication, ``/`` for division, ``%`` for modulo, and ``^`` for exponentiation. + +Integers are defined as you would expect and Floats with the decimal point. Ex:: 0 1 18374 2.5 -However, negative numbers do not use a unary version of the subtraction symbol since all operators have a fixed arity (more on that later) but use the unary reversal operator: ``_``:: +However, negative numbers do not use a unary version of the subtraction symbol since all operators have a fixed arity (more on arity later). Instead, negative numbers use the unary reversal operator: ``_``:: Invalid: -25 Valid: _25 -Pyth also has predefined variables that are set to useful values before program execution starts. Examples are:: +Pyth also has many predefined variables:: Z=0 T=10 @@ -48,30 +50,28 @@ Pyth also has predefined variables that are set to useful values before program d=" " b="\n" -All operators, variables, and control flow keywords, are always one character long to reduce the size of the program. This does make Pyth programs very hard to read. - -As I said before, Pyth has much more than arithmetic, but you'll learn about them in due time. +All operators, variables, and control flow keywords, are always one character long to reduce the size of the program. 2.3. All Operators in Pyth Have a Fixed Arity ============================================= -A very important concept in Pyth is that of arity. The `arity `_ of an operator or function (according to Wikipedia) is *the number of arguments or operands the function or operation accepts.* Most programming languages allow functions to accept different amounts of arguments, but this causes them to require parenthesis. In the want for smaller programs, Pyth has a fixed number of operands per operator, allowing it to do away with parenthesis or any other delimiter. +The `arity `_ of an operator or function (according to Wikipedia) is *the number of arguments or operands the function or operation accepts.* Most programming languages allow functions to accept different numbers of arguments, but doing so requires parenthesis. Pyth instead has a fixed number of operands per operator, allowing it to do away with parenthesis or any other delimiter. 2.4. Operators Mean Different Things in Different Contexts ========================================================== -In an effort to further increase the number of function available with only one character operators, operators do different things in Pyth depending on the values passed to it. For example, the ``+`` operator adds numbers, which you already saw, but if it gets two sequences (e.g. strings, lists) as operands, it concatenates them:: +To further increase the number of functions available with only one character operators, operators operate differently depending on the values passed to it. For example, we saw the ``+`` operator adds numbers, but if ``+`` gets two sequences (e.g. strings, lists) as operands, it concatenates them:: +2 T -> 12 (remember that T=10) +"Hello ""World!" -> Hello World! -The ``+`` sign meaning both concatenation and addition is pretty common, but Pyth takes this to another level, with most operators having 2 or more meanings. +Letting ``+`` mean both concatenation and addition is pretty common, but Pyth takes this to another level, with most operators having 2 or more meanings. 2.5. The Pyth Code is Compiled to Python Code Then Run ====================================================== -Pyth is technically a `JIT `_ compiled language since the Pyth code is converted to Python through a series of rules defined in the file ``data.py`` and then run with the variables and functions defined in ``macros.py``. To see this in action, you can select the debug button in the online interpreter or pass the ``-d`` flag to the local one to see the compiled Python code. Here is what comes out as debug from ``^T6`` which evaluates to a million:: +Pyth is technically a `JIT `_ compiled language since the Pyth code is converted to Python through a series of rules defined in the file ``data.py`` and then run with the variables and functions defined in ``macros.py``. You can select the debug button in the online interpreter or pass the ``-d`` flag to the local one to see the compiled Python code. Here is what comes out as debug from ``^T6`` which evaluates to a million:: ================================================== ^T6 @@ -81,5 +81,3 @@ Pyth is technically a `JIT