How do you replace existing operators without invoking them in Io? - operators

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?

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

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.

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)))

Related

OCaml : Infinite Loop

I am trying to do this simple while loop but it's not working. The compiler is not giving any type of error or warning but when i try to run my function it ends up in an infinite loop:
let example x =
let k = ref x in
while (!k > 42) do
if ( (!k mod 5) == 0) then (
k:= !k/2
);
done;
if(!k<42) then (
Printf.printf "k is less than 42"
);
if(!k == 42) then (
Printf.printf "k is equal to 42"
)
;;
Well, your loop only modifies !k when !k mod 5 = 0. If !k isn't divisible by 5, it will never change in value. This suggests that the loop will either run 0 times or will run an infinite number of times.
You don't show any calls to example, but any call where you pass a value > 42 and not a multiple of 5 should loop infinitely it seems to me.
By the way, this is what #user2864740 was trying to point out. 43 is a value > 42 that's not divisible by 5.
(As a side comment, you should use = to compare values for equality. The == operator in OCaml is useful in limited cases. This often causes problems for people coming from other languages.)

How do I represent the infinite number ~0.99999..... in Objective-C / iOS?

I have a number that could potentially go on forever, like ~0.999999...
See this:
while (myNumber / 25 > 0.999999)
{
// do stuff
}
How can I represent that number in Objective-C properly?
Or maybe I am doing this while loop incorrectly and someone can tell me how to fix it? I'm trying to see if myNumber can be divided by 25 at least once... so even 26/25 would pass (mod % wouldn't work in this case).
Fortunately for you, this repeating decimal has an exact representation in floating point arithmetic (you don't have to round):
while (myNumber / 25 > 1)
{
// do stuff
}
This is because "0.999999..." and "1" are just different ways of writing the same number. They are not different numbers.
Or perhaps you want to do this:
while (myNumber / 25 >= 1)
If myNumber is a non-negative integer, then this is actually the same as:
while (myNumber / 25) // nonzero values are "true"
Or, you can translate it to the equivalent:
while (myNumber >= 25) // no need to divide
If myNumber is greater than or equal to 25, then it can be divided by 25 at least once.

Fast way to find the next multiple of a number

I need to find the first multiple for a number starting from a base number. For example: The first multiple of 3 from 7 is 9. My first attempt was to do this:
multiple = baseNumber
while(multiple%number !=0 )
multiple++
At the end, "multiple" will have the first multiple of number after baseNumber. The problem is that when number becomes too large, the number of iterations becomes too many. So my question is: is there a faster way to do this?
If everything is guaranteed to be positive, try
multiple = baseNumber + number - 1;
multiple -= (multiple % number);
That does it in constant time.
First, we add number - 1 to make sure that we have a number at least as large as the next multiple but smaller than the one after that. Then we subtract the remainder of the division by number to make sure we have the desired multiple.
If baseNumber can be negative (but number still positive), we face the problem that multiple % number may be negative if multiple < 0, so the above could skip a multiple of number. To avoid that, we can use e.g.
remainder = multiple % number;
if (remainder < 0) remainder += number;
multiple -= remainder;
If branching is too expensive, we can avoid the if at the cost of two divisions instead of one,
multiple -= (number + (multiple % number)) % number;
Generally, the if seems preferable, though.
If number can be negative, replace it with its absolute value first.
Note: The above returns, as the original code does, baseNumber if that is already a multiple of number. If that isn't desired, remove the - 1 in the first line.
try this (Requires INTEGER division):
multiple = ((base/number) + 1) * number;
7/3 = 2. 3*(2+1) = 9.
You have an edge case where the baseNumber already is a multiple of number, which you will have to test using the modulus operation.
Why do you need a loop?
multiple = (floor(number/baseNumber)+1)*baseNumber
while(multiple * number < baseNumber)
multiple++;
so for baseNumber = 3, number = 7, your multiple is 3;
though, something tells me bignums are about to show up in here.

Smalltalk syntax error

I'm learning smalltalk right now and am trying to make a very simple program that creates an array of numbers and then finds the largest number. My code looks like this:
| list max |
list := #(1 8 4 5 3).
1 to: list size do: [:i |
max < (list at: i)
ifTrue: [max := (list at: i)].
ifFalse: [max := max].
].
When I run this code, I get "stdin:7: parse error, expected ']'". I'm a bit confused as to what is causing this. It looks to me like all of my square brackets correspond. Help?
Alexandre told you already that is likely that collection provides a max method. You might be interested in a few ways how to do it.
Using collection max (maximum of all elements)
#(1 8 4 5 3) max
Using number max: (which of two numbers is bigger)
#(1 8 4 5 3) inject: 0 into: [:max :elem|
max max: elem ]
Or using just using the internal iterator
#(1 8 4 5 3) inject: 0 into: [:max :elem|
max < elem
ifTrue: [ elem ]
ifFalse:[ max ] ]
Together with your solution to use an external iteration you can see there are a lot of possibilities.
Hope it adds something
The line number seems off but looking at your code it seems the error is probably caused by the period after ifTrue: [max := (list at: i)]. #ifTrue:ifFalse is a single method selector and it doesn't make sense to break it into two statements.
Actually you could remove the ifFalse part of the code entirely. Assigning max to itself has no effect. Also looping on the indices is not needed here. You can work on the values directly with list do: […].
The max variable should also be initialized. Zero seems like a good choice to compare against the positive numbers in your array.
But instead of doing all that look into the Collection class. Your Smalltalk dialect may already offer a max method for this task.
Your immediate problem is that you terminate the ifTrue section with a period. You could normally just remove the period but, since your ifFalse section is effectively a non-operation, it's probably better to remove that bit.
But even when you fix that, you still need to initialise max so that you don't get a < message being sent to the nil object. You can initialise it to the first element, if there are any, then you can change the loop to start at the second.
Of course, initialising to the first element is problematic when the list is empty so you should handle that as well. In the code below, I've set it to a suitable small value then initialised it from the first element of the list only if it's available.
Corrected code is:
| list max |
list := #(1 8 4 5 3).
max := -99999.
(list size) > 0 ifTrue: [max := (list at: 1)].
2 to: list size do: [:i |
max < (list at: i) ifTrue: [max := (list at: i)].
]
(max) displayNl
This outputs 8 as expected and also works fine on the edge cases (list size of zero and one).

Recognizing when to use the modulus operator

I know the modulus (%) operator calculates the remainder of a division. How can I identify a situation where I would need to use the modulus operator?
I know I can use the modulus operator to see whether a number is even or odd and prime or composite, but that's about it. I don't often think in terms of remainders. I'm sure the modulus operator is useful, and I would like to learn to take advantage of it.
I just have problems identifying where the modulus operator is applicable. In various programming situations, it is difficult for me to see a problem and realize "Hey! The remainder of division would work here!".
Imagine that you have an elapsed time in seconds and you want to convert this to hours, minutes, and seconds:
h = s / 3600;
m = (s / 60) % 60;
s = s % 60;
0 % 3 = 0;
1 % 3 = 1;
2 % 3 = 2;
3 % 3 = 0;
Did you see what it did? At the last step it went back to zero. This could be used in situations like:
To check if N is divisible by M (for example, odd or even)
or
N is a multiple of M.
To put a cap of a particular value. In this case 3.
To get the last M digits of a number -> N % (10^M).
I use it for progress bars and the like that mark progress through a big loop. The progress is only reported every nth time through the loop, or when count%n == 0.
I've used it when restricting a number to a certain multiple:
temp = x - (x % 10); //Restrict x to being a multiple of 10
Wrapping values (like a clock).
Provide finite fields to symmetric key algorithms.
Bitwise operations.
And so on.
One use case I saw recently was when you need to reverse a number. So that 123456 becomes 654321 for example.
int number = 123456;
int reversed = 0;
while ( number > 0 ) {
# The modulus here retrieves the last digit in the specified number
# In the first iteration of this loop it's going to be 6, then 5, ...
# We are multiplying reversed by 10 first, to move the number one decimal place to the left.
# For example, if we are at the second iteration of this loop,
# reversed gonna be 6, so 6 * 10 + 12345 % 10 => 60 + 5
reversed = reversed * 10 + number % 10;
number = number / 10;
}
Example. You have message of X bytes, but in your protocol maximum size is Y and Y < X. Try to write small app that splits message into packets and you will run into mod :)
There are many instances where it is useful.
If you need to restrict a number to be within a certain range you can use mod. For example, to generate a random number between 0 and 99 you might say:
num = MyRandFunction() % 100;
Any time you have division and want to express the remainder other than in decimal, the mod operator is appropriate. Things that come to mind are generally when you want to do something human-readable with the remainder. Listing how many items you could put into buckets and saying "5 left over" is good.
Also, if you're ever in a situation where you may be accruing rounding errors, modulo division is good. If you're dividing by 3 quite often, for example, you don't want to be passing .33333 around as the remainder. Passing the remainder and divisor (i.e. the fraction) is appropriate.
As #jweyrich says, wrapping values. I've found mod very handy when I have a finite list and I want to iterate over it in a loop - like a fixed list of colors for some UI elements, like chart series, where I want all the series to be different, to the extent possible, but when I've run out of colors, just to start over at the beginning. This can also be used with, say, patterns, so that the second time red comes around, it's dashed; the third time, dotted, etc. - but mod is just used to get red, green, blue, red, green, blue, forever.
Calculation of prime numbers
The modulo can be useful to convert and split total minutes to "hours and minutes":
hours = minutes / 60
minutes_left = minutes % 60
In the hours bit we need to strip the decimal portion and that will depend on the language you are using.
We can then rearrange the output accordingly.
Converting linear data structure to matrix structure:
where a is index of linear data, and b is number of items per row:
row = a/b
column = a mod b
Note above is simplified logic: a must be offset -1 before dividing & the result must be normalized +1.
Example: (3 rows of 4)
1 2 3 4
5 6 7 8
9 10 11 12
(7 - 1)/4 + 1 = 2
7 is in row 2
(7 - 1) mod 4 + 1 = 3
7 is in column 3
Another common use of modulus: hashing a number by place. Suppose you wanted to store year & month in a six digit number 195810. month = 195810 mod 100 all digits 3rd from right are divisible by 100 so the remainder is the 2 rightmost digits in this case the month is 10. To extract the year 195810 / 100 yields 1958.
Modulus is also very useful if for some crazy reason you need to do integer division and get a decimal out, and you can't convert the integer into a number that supports decimal division, or if you need to return a fraction instead of a decimal.
I'll be using % as the modulus operator
For example
2/4 = 0
where doing this
2/4 = 0 and 2 % 4 = 2
So you can be really crazy and let's say that you want to allow the user to input a numerator and a divisor, and then show them the result as a whole number, and then a fractional number.
whole Number = numerator/divisor
fractionNumerator = numerator % divisor
fractionDenominator = divisor
Another case where modulus division is useful is if you are increasing or decreasing a number and you want to contain the number to a certain range of number, but when you get to the top or bottom you don't want to just stop. You want to loop up to the bottom or top of the list respectively.
Imagine a function where you are looping through an array.
Function increase Or Decrease(variable As Integer) As Void
n = (n + variable) % (listString.maxIndex + 1)
Print listString[n]
End Function
The reason that it is n = (n + variable) % (listString.maxIndex + 1) is to allow for the max index to be accounted.
Those are just a few of the things that I have had to use modulus for in my programming of not just desktop applications, but in robotics and simulation environments.
Computing the greatest common divisor
Determining if a number is a palindrome
Determining if a number consists of only ...
Determining how many ... a number consists of...
My favorite use is for iteration.
Say you have a counter you are incrementing and want to then grab from a known list a corresponding items, but you only have n items to choose from and you want to repeat a cycle.
var indexFromB = (counter-1)%n+1;
Results (counter=indexFromB) given n=3:
`1=1`
`2=2`
`3=3`
`4=1`
`5=2`
`6=3`
...
Best use of modulus operator I have seen so for is to check if the Array we have is a rotated version of original array.
A = [1,2,3,4,5,6]
B = [5,6,1,2,3,4]
Now how to check if B is rotated version of A ?
Step 1: If A's length is not same as B's length then for sure its not a rotated version.
Step 2: Check the index of first element of A in B. Here first element of A is 1. And its index in B is 2(assuming your programming language has zero based index).
lets store that index in variable "Key"
Step 3: Now how to check that if B is rotated version of A how ??
This is where modulus function rocks :
for (int i = 0; i< A.length; i++)
{
// here modulus function would check the proper order. Key here is 2 which we recieved from Step 2
int j = [Key+i]%A.length;
if (A[i] != B[j])
{
return false;
}
}
return true;
It's an easy way to tell if a number is even or odd. Just do # mod 2, if it is 0 it is even, 1 it is odd.
Often, in a loop, you want to do something every k'th iteration, where k is 0 < k < n, assuming 0 is the start index and n is the length of the loop.
So, you'd do something like:
int k = 5;
int n = 50;
for(int i = 0;i < n;++i)
{
if(i % k == 0) // true at 0, 5, 10, 15..
{
// do something
}
}
Or, you want to keep something whitin a certain bound. Remember, when you take an arbitrary number mod something, it must produce a value between 0 and that number - 1.