32

I’m trying to complete the second exercise on IO day 2 in the book Seven Languages in Seven Days. In it your asked, “How would you change / to return 0 if the denominator is zero?” I've determined that I can add a method to Number using:

Number new_div := method(i, if(i != 0, self / i, 0))

What I’m not sure is how to replace the ”/” in the operator table. I’ve tried:

Number / := Number new_div
Number / := self new_div

But I get an exception to both as it’s trying to invoke ”/”. How do I get a handle on Number / so I can store a reference to the old method and then redefine it for my own purposes? Am I going about this all wrong?

draegtun
  • 22,441
  • 5
  • 48
  • 71
Mark B
  • 2,870
  • 3
  • 20
  • 18
  • I am trying to solve the same problem. I do "Number / := method(...)". And I'm stuck with the same infinite loop that jer pointed out. I can't find a way to preserve the original implementation of / and call it. – Eric Hogue Nov 24 '10 at 02:25
  • @Eric Hogue: I didn't post my answer because *jer* just beat me to it! I've posted it now because it does show how to preserve original division and avoid recursion loop. – draegtun Nov 24 '10 at 12:14

3 Answers3

40

For Eric Hogue (see question comments):

origDiv := Number getSlot("/")

10 origDiv(5) println   # => 2
10 origDiv(0) println   # => inf

Number / := method (i, 
    if (i != 0, self origDiv(i), 0)
)

(10 / 5) println        # => 2
(10 / 0) println        # => 0
AndyG
  • 39,700
  • 8
  • 109
  • 143
draegtun
  • 22,441
  • 5
  • 48
  • 71
4

What you want to do is run:

Number setSlot("/", Number getSlot("new_div")

For example.

However, it should be noted, you'll have an infinite loop on your hands if you use that definition of new_div, since you're calling the / method within it, and setting the / operator to use new_div will cause the call to, 6 / 2 to recurse until you run out of memory.

jer
  • 20,094
  • 5
  • 45
  • 69
  • 1
    How does that help? You'll still have an infinite loop. – TrueWill Mar 06 '12 at 18:25
  • Did you even read what I wrote? I explicitly make a note about the potential for an infinite loop. – jer Mar 08 '12 at 15:57
  • 1
    There's no "potential" here. You **will** have an infinite loop unless you go Johan's route and re-implement division or go @draegtun's route and save the original implementation. And you're missing a closing parenthesis. The other answers provide complete implementations, which your answer does not. – TrueWill Mar 08 '12 at 19:43
  • 2
    And in my answer I didn't say anything about potential, did I? You're bitching about something I explicitly noted in the first revision of my answer. You don't have to teach me about the internals of Io, I've been a core developer of the language since 2003. Thanks. – jer Mar 08 '12 at 20:15
4

What if you used the power operator inside your redefinition, then you don't have to keep a reference to the old division operator.

Number / := method(i, if(i==0, 0, self*i**(-1)))
Johan
  • 41
  • 2
  • 2
    That assumes that the old behaviour is the default behaviour. Someone else may have changed it on you. Be a good citizen, always look up the current behaviour when you want to replace it. :) – jer Sep 30 '11 at 17:03