• 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:

    Fireside Friday, November 27, 2020

    Hey folks! Fireside this week. A bit of a change-up in terms of the coming attractions. I had planned to start “Textiles, How Did They Make It?” next, but I want to do a bit more reading on some of the initial stages of textile production (that is, the pr…

    via A Collection of Unmitigated Pedantry November 27, 2020

    Building Depth and Window Space

    How much window space does an apartment need, relative to its area, and how does this affect building style? A fascinating post from about a year ago on Urban Kchoze makes the argument that modern North American buildings are too deep – Simon calls them o…

    via Pedestrian Observations November 27, 2020

    Thoughts you mightn't have thunk about remote meetings

    Welcome to this week's edition of "building a startup in 2020," in which all your meetings are suddenly remote, and you probably weren't prepared for it. I know I wasn't. We started a "fully remote" company back in 2019, but …

    via apenwarr November 23, 2020

    more     (via openring)


  • Posts
  • RSS
  • ◂◂RSS
  • Contact