q a n = (( a ^ n ) - 1 ) div ( a - 1 );
for n and a >= 2 theoretically allways gives an integer value. But, the
whole function only returns floats. This is, because ^ always results
to a float, am I right?
Can I somehow "reduce" ^ to only use ints? Do I have to overload ^. If
so, how do I do that? If the easiest way is to rewrite ^ to a only int
function, I did that allready:
pow x n = loop n with loop n = x * loop (n-1) if n > 0; = 1; end;
q a::int n::int = ((pow a n) - 1 )div(a - 1);
This works as expected. But as I get confused with the type thing
in pure quite sometimes, it would be nice if sb could help me with
this very problem. I think, some basic words could help me alot
understanding how to handle different types in pure.
Many thanks in advance,
Bjoern
Yes. But there's also a function named pow in the prelude which already
does what you want. Because powers tend to get big, it returns bigints,
though:
> pow 2 100;
1267650600228229401496703205376L
(Note the L at the end of the number. It indicates a bigint, pretty much
like in Python.)
If your result is small enough, you can then just convert it back to an
ordinary, 32 bit machine int with the 'int' conversion function, without
losing information:
> pow 2 15;
32768L
> int ans;
32768
> Can I somehow "reduce" ^ to only use ints? Do I have to overload ^.
Well, you can't really redefine the prelude (^) function on ints,
because the definition in the prelude already covers that case -- the
equations defining (^) on int come "before" those that you would add,
and hence take priority.
But you can define your own version of (^) in a separate namespace. Here
I use the 'mypow' namespace:
> namespace mypow;
> infixr (::^) ^;
> x::int ^ y::int = int (pow x y);
> x ^ y = x ::^ y otherwise;
> 2^15;
32768
> 2.0^15;
32768.0
Note that the infixr declaration gives your new ^ operator the same
associativity and precedence as the prelude one. Then comes an equation
defining the case that you're interested in. The second equation, x ^ y
= x ::^ y otherwise, makes your ^ operator use the standard (prelude)
definition of ^ (denoted as ::^, which tells the compiler that we mean
the ^ in the default namespace there) if the operands aren't ints. This
is convenient if you want to have the new operator work exactly like the
old one except in your special case.
Of course, you can put the above two equations into a script mypow.pure,
say:
namespace mypow;
infixr 2500 ^;
x::int ^ y::int = int (pow x y);
x ^ y = x ::^ y otherwise;
You can then just import that script and switch to the mypow namespace with:
using mypow;
using namespace mypow;
After that, '^' will refer to your version of the operator, whose fully
qualified name is actually mypow::^.
(The difference between 'namespace mypow' and 'using namespace mypow' is
that the former allows you to define things in the mypow namespace,
while the latter only makes the public symbols from mypow available as
unqualified symbols in the current context. You can find all the gory
details about this namespaces stuff in the manual, see
http://docs.pure-lang.googlecode.com/hg/pure.html#namespaces.)
> I think, some basic words could help me alot
> understanding how to handle different types in pure.
Well, it would help if you could narrow down your problem just a
*little* bit more. ;-) What exactly confuses you? The order in which
different equations are taken into consideration? The set of types
available in Pure? The numeric types in particular? The type tag
notation (x::int)?
Albert
--
Dr. Albert Gr"af
Dept. of Music-Informatics, University of Mainz, Germany
Email: Dr.G...@t-online.de, a...@muwiinfa.geschichte.uni-mainz.de
WWW: http://www.musikinformatik.uni-mainz.de/ag
> (The difference between 'namespace mypow' and 'using namespace mypow'
> is that the former allows you to define things in the mypow
> namespace, while the latter only makes the public symbols from mypow
> available as unqualified symbols in the current context. You can find
> all the gory details about this namespaces stuff in the manual, see
> http://docs.pure-lang.googlecode.com/hg/pure.html#namespaces.)
Ups, sorry. I did not look at namespaces. So, this is the case to use
those. So I learned this. Thank you!
> > I think, some basic words could help me alot
> > understanding how to handle different types in pure.
>
> Well, it would help if you could narrow down your problem just a
> *little* bit more. ;-) What exactly confuses you? The order in which
> different equations are taken into consideration? The set of types
> available in Pure? The numeric types in particular? The type tag
> notation (x::int)?
Well your mail answered my question perfectly. Thinking about it, I
think the thing that confuses me is, that I cannot control the type a
function is returning. It is determened by the functions contained,
right? This is differen from what I learned in C and C++. But I think
I'm just about to get used to it.
Please excuse, that you have to explain basic concepts of programming
here. It is very helpfull for me to have the opportunity to ask these
questions. I hope this might be helpfull for outhers to, eventually.
Best,
Bjoern
Pure is dynamically typed like Lisp and Python, see
http://en.wikipedia.org/wiki/Type_system#Dynamic_typing. If you are used
to statically typed languages like C/C++, dynamic typing takes some time
to get used to. At first it's a bit like walking a rope with no strings
attached and no safety net. But see
http://www.lisperati.com/landoflisp/panel01.html. ;-)
> Please excuse, that you have to explain basic concepts of programming
> here. It is very helpfull for me to have the opportunity to ask these
> questions. I hope this might be helpfull for outhers to, eventually.
No problem, that's what this list is for. Just go ahead.