• Posts
  • RSS
  • ◂◂RSS
  • Contact

  • Significant Whitespace In Expressions

    January 8th, 2011
    ideas, math, programming  [html]
    Order of operations can conflict with what an expression looks like it should mean. For example, the expression a * b+c looks like it means a*(b+c) but technically (and in most computer languages) means (a*b)+c. This seems analagous to the reasoning behind python using indentation instead of braces. The C code:
      if (x)
         if (y)
           z();
      else
         w();
    
    looks like it should mean
      if (x) {
        if (y) {
          z();
        }
      }
      else {
        w();
      }
    
    but actually means
      if (x) {
        if (y) {
           z();
        }
        else {
          w();
        }
      }
    
    Python deals with this by treating the whitespace as significant, so:
      if x:
        if y:
          z()
        else:
          w()
    
    and
      if x:
        if y:
           z()
      else:
        w()
    
    both mean what they look like they should mean.

    Some examples of how I would propose we do things instead:

      write                 to mean
      -----                 -------
      a+b                   a+b
      a + b*c               a+(b*c)
      a * b+c               a*(b+c)
      a+b * c+d             (a+b)*(c+d)
      a+b - c+d             (a+b)-(c+d)
      a+b  *  c+d - e+f     (a+b) * ((c+d) - (e+f))
      a  *  c + d + e       a*(c+d+e)
      a + b                 a+b, maybe yields warning
      a-b+c                 error
      a*b+c                 error
    
    Algorithm:
    for each number of spaces 0 on up:
      group any terms with that number of internal
      spaces by adding parens
    

    Comment via: facebook

    Recent posts on blogs I like:

    The Limit of Circles in the Suburbs

    In dense urban cores, it’s valuable to run circular rail lines. They connect dense near-center neighborhoods to one another without going through the more congested center, and help make transferring between parallel lines more efficient, again through av…

    via Pedestrian Observations September 6, 2020

    Collections: Bread, How Did They Make It? Addendum: Rice!

    As an addendum on to our four-part look at the general structures of the farming of cereal grains (I, II, III, IV) this post is going to briefly discuss some of the key ways that the structures of rice farming differ from the structures of wheat and barle…

    via A Collection of Unmitigated Pedantry September 4, 2020

    Notes on “Anthropology of Childhood” by David Lancy

    I read David Lancy’s “The Anthropology of Childhood: Cherubs, Chattel, and Changelings” and highlighted some passages. A lot of passages, it turns out. [content note: discussion of abortion and infanticide, including infanticide of children with disabilit…

    via The whole sky August 27, 2020

    more     (via openring)


  • Posts
  • RSS
  • ◂◂RSS
  • Contact