How to add in programming languages


This is a tongue-in-cheek attempt to expose programming language expressiveness.

1. Scheme

(+ 1 2)
(+ 1 2 3 4)
  • Characters needed to add n digits: + 3
  • Prefix notation allows passing in multiple operands
  • Prefix notation is not popular

2. JavaScript/Python/Ruby/Octave/Matlab

1 + 2
  • Characters needed to add n digits: + (n – 1)
  • Very simple and generally accepted
  • Infix form requires two operands leading to (n – 1) operators

3. Java/C#

// CSharp
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(1 + 2);
    }
}
  • Characters needed to add n digits: N (where N is a largeeeeeeee number :) )
  • Too verbose – you need a class to add two numbers

4. SQL

SELECT 1 + 2;
  • Characters needed to add n digits: + 8
  • Simple but not popular
  • Declarative nature can impact clarity

Do you know of any other language examples? Feel free to share in the comments!

Here are a couple of other posts you might also like:

13 thoughts on “How to add in programming languages

  1. Forth would be 2 1 + for 2 numbers and 2 1 3 + + or 2 1 + 3 + for 3 numbers. Each operation pops x operands and returns the result pushed onto the stack like the old HP calculators. I haven’t used it in years. It’s a stack oriented programming language.

    Like

  2. Python: 1 + 2
    Just like JavaScript. Added Con for both:
    * If the order of evaluation might matter, you better know it.
    Added Con for Python:
    * You better know how types interact and which types result, because 2/3. = 0.6666666666666666, but 2/3 = 0

    Like

  3. The differences are largely according to infix, prefix, or postfix. The latter two were come upon as a way to avoid having to use parentheses. The LISP family of (scheme is one of it’s descendants) use prefix (also known as polish) notation as well as a few others like Tcl. Makes it sound a lot like the Celtic and Semitic languages (VSO). The postfix form (SOV) is like the turkic languages and postscript is probably the most commonly known example.
    Lets not forget about adding using boolean gates, which is happening behind the scenes:
    sum=1^2^c
    c’=(1&2)^((1^2)&c)

    Like

    1. Brilliant insight Paul!

      I absolutely love the way you related this to real human languages and the the subject-object-verb placements. I love languages too but didn’t think of this connection.

      Yes most of the differences come from the infix, prefix and postfix notation and there are a other of syntax changes too. All the same, great feedback!

      Like

  4. I haven’t done assembler in so long I forget the details, but in some simulated variety of assembler:

    MOV R1, #1 !! store a 1 in register 1
    ADD R1, #2 !! add a 2 to register 1

    Of course, this assumes you can then make use of the result out of R1, rather than having to spend more cycles moving it somewhere.

    Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.