The Perl 6 language has a symbol to represent infinity (∞). Anyone have any ideas on how I can implement it in nim? Just for fun!
Use cases:
import math
import strformat
proc is_prime(n: int64): bool =
for i in 2 .. n-1:
if (n mod i) == 0:
return false
return if n != 1 and n != 0: true else: false
for number in 1 .. ∞:
if is_prime(number):
echo &"{number} is prime number"
Loop from the beginning of the universe to the end of the universe (assuming the universe is infinite).
for age in -∞ .. ∞:
echo if age < 0: "Before Christ" elif age == 0: "Year Zero" else: "Anno Domini"
I assume you realize that your second example will not get to "Year Zero" since it sure looks like you are requesting an infinite number of B.C. years?
Not sure you want to build an entire lazy evaluation system and symbology like Perl6..It's easy to do an infinite iterator by just doing your own iterator called fromXtoInf https://nim-lang.org/docs/tut1.html#iterators (and you could put that unicode ∞ symbol in the iterator name if you want, but you might need to backquote the symbol). I'm really not sure, though, how many would think that is clearer than simply:
number = 0
while true:
number.inc
...
Have fun
const ∞ = high(BiggestInt)
echo ∞
Thanks @leorize it's so easy! and work :-)
const ∞ = high(BiggestInt)
echo ∞
for number in -∞ .. ∞:
echo number
@LeuGim - nicely done & interesting approach!
I'm not sure I would have embued inf(0)|Infinite(0) with as precise semantics in your for 3 .. inf(0): ... construction. Infinity is more a process than a number. For ∞ - ∞ ~ inf(0) to act like zero the two limiting processes have to precisely cancel out. In some abstract sense, that's much less { infinitely less? :-) } likely than being lopsided and the result being more like either -∞ or +∞. Without more context there's not enough to know which (or zero). So, being undefined is probably a better choice.
Also, for triple bonus extra credit along these lines you could try to do the point at infinity in the complex plane/Riemann sphere. ;-) { https://en.wikipedia.org/wiki/Riemann_sphere }
Thank you @LeuGim for your effort in dealing with infinity... even if not really.
I remembered another symbol with infinite decimal places (π), but there are many more.
import math
# const π = 3.141592653589793...
const π = PI
echo π
These symbols are universally recognized and thanks to the unicode can be used in in programming. It would be cool to have a module with just these alternative symbols for mathematical constants.Hi, @mrhidas.
Yes, some mathematics-related code could be prettier. Of coarse the harder part is to get into the way of typing them. But not hopeless I think. :)
But not to overestimate what can be done:
import math
proc √*(n: float): float = sqrt(n)
assert (√ 25) == 5.0 # we need space after ``√``, yet parentheses for precedence
proc √*(p, n: float): float = pow(n, 1/p)
# assert 3 √ 8 == 2 # won't be parsed, ``√`` is not treated as infix operator symbol
proc ²*(n: SomeNumber): n.type = n*n
# assert 5² == 25 # won't be parsed, no postfix operators
Just first mathematical symbols, which came to mind.
But I guess you still are going to do this, https://en.wikipedia.org/wiki/List_of_mathematical_symbols_by_subject is for some help (at least to copy symbols). You may need to re-export some stuff along with your defined symbols, so that users don't need to import the initial module yet, use export for that.
Typing these unicode symbols from the keyboard is not a big problem
From the wikipedia: "For example, GTK+ is an ISO/IEC 14755-conformant system[citation needed]. The beginning sequence is Ctrl+⇧ Shift+U and the ending sequence is ↵ Enter or Space. Programs based on GTK+, such as GNOME applications, support Unicode input."
It is even possible to search Google for the result of √2 or 4 ÷ 2
I said it's not hopeless. On windows some symbols are even easier to type, just 1-4 numbers on number pad, while holding Alt — say this em-dash; you won't remember all codes, but 20-40 is realistic.
But it doesn't help you to use proc √*(p, n: float): float, as in the example, even just proc √2.
(anyway, I didn't discourage you, you may gather what can work, e.g. constants like π; yet there's not much of these in StdLib; so probably you'll need to gather many such mini-modules to translate into mathematical notation different packages)
One way to merge all these mini-modules with translations into one file:
# include-file ``math_notation.nim``
when declared math.PI:
const π = PI
const τ = TAU
# and so on
when declared someAnotherModuleToTranslate.someExportedSymbolFromIt:
template ⌂(x) = someAnotherModuleToTranslate.someExportedSymbolFromIt(x)
# and so on
# and so on
Then you include this file (not import it), where you want to use these translations, after importing modules, which you need.
# module test.nim
import math # we need just ``PI`` here, and we don't need to import all other modules
include math_notation
echo π
when false:
⌂ 5 # this would not compile here, ``someAnotherModuleToTranslate`` not imported
Using that an include-file vs. module allows it to see what modules are imported in the user module, and to add translations only to their symbols; i.e. no dependency on all translated modules (they may be not standard and unrelated one to another).