Loading ...
Sorry, an error occurred while loading the content.

224Re: [extremeperl] Terrence the Hypocrite

Expand Messages
  • Adam Turoff
    Apr 4, 2005
    • 0 Attachment
      On Apr 3, 2005 4:49 PM, Terrence Brannon <bauhaus@...> wrote:
      > Not two full days after I call for the head of Rob Nagler for leading
      > people into the heinous act of copying code, do I do it myself, albeit
      > in Haskell:
      > twoExp n
      > | n == 1 = 2
      > | even n = (twoExp m) ** 2
      > | odd n = 2 * (twoExp m) ** 2
      > where m = n `quot` 2
      > I did it because I felt that the expression:
      > (twoExp m) ** 2
      > was far more readable that abstracting it for "re-use"

      I don't know about that. I find this version more readable:

      twoExp n
      | n == 1 = 2
      | even n = x
      | odd n = 2 * x
      where m = n `quot` 2
      x = (twoExp m) ** 2

      In the Forth world, the most important tool a programmer needs is a
      thesaurus. If you can name a concept, you have identified it (_and_
      given it an identity -- a single implementation), and you can refer to
      it as necessary. This obviously works better when you can compose
      higher order operations from smaller ones. Don't try this with COBOL. ;-)

      I don't have a good name for the task you are trying to accomplish
      recursively, so I'm just using the name 'x' here. This should be named
      better, but (1) the scope is quite small (so a name like 'm' or 'x' does
      not require much in the way of disambiguation), and (2) it's a trivial
      refactoring that should take on the order of 30 seconds.

      The key here is to waste less time arguing over what and when to
      refactor, and just refactoring when you see a problem. (Provided you
      have a good test suite to prove your refactoring reasonably correct,
      and your refactorings take closer to 30 seconds than 30 days.)

      > After claiming that testing was irrelevant in Haskell, I am now at
      > Section 4.5 in the best Haskell instructional text I have seen, "Craft
      > of Functional Programming" by Thompson, and what is this chapter
      > called: "Program Testing". So, there we go. Two feet in my mouth in
      > less than 2 days.

      I think the more important point here is that your function is undefined
      for n <= 0. That's a pretty serious bug. It catches the case where n
      is non integral thanks to the type system though. ;-)

      Finally, I want to point out that this whole line of discussion has an
      angels-dancing-on-a-pin quality to it, and serves as proof by example
      of Torkington's Law[1].

      -- Adam

      [1] Email is a vast, global conspiracy to waste _your_ time.
    • Show all 13 messages in this topic