• Posts
  • RSS
  • ◂◂RSS
  • Contact

  • Significant Whitespace In Expressions

    January 8th, 2011
    ideas, math, programming, tech
    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:

    A Big Problem With The Going To Bed Book

    One day my dad was reading this book called the "Going to Bed Book" to my sister Nora. The book is basically about a bunch of animals who are getting ready for bed on a boat. They go down the stairs, take a bath, hang their towels on the wall, find…

    via Lily Wise's Blog Posts September 18, 2023

    Investing in boundaries with young kids

    Putting in some work to get the behavior you want The post Investing in boundaries with young kids appeared first on Otherwise.

    via Otherwise August 15, 2023

    Self-driving car bets

    This month I lost a bunch of bets. Back in early 2016 I bet at even odds that self-driving ride sharing would be available in 10 US cities by July 2023. Then I made similar bets a dozen times because everyone disagreed with me. The first deployment to pot…

    via The sideways view July 29, 2023

    more     (via openring)


  • Posts
  • RSS
  • ◂◂RSS
  • Contact