determining if a int is a power

hello all, im new to haskell and have to do some excersises. Well i ran into this problem. I have to determin if a list of int's contains a int that is a power. So i have been trying using "any" with a helper function which return a Bool if the int is a power and applying that to the list. I think that this aproach should work. Anyway the problem im having is in the helper function. I have been trying this: isPower n = let powerF = floor(sqrt(n)) in if (powerF*PowerF == n) then True else False but this doesnt work. Prelude says that the definition of isPower needs a fractional int (if i remember correctly) anyway it looks like something is wrong with my type's? Could you guys help me out? Is there a simple standard function i overlooked. Or what should i change? Thanks in advance guys/gals

try "floor (sqrt (fromIntegral n))" On Fri, 7 Nov 2003 ddekker6@chello.nl wrote:
hello all,
im new to haskell and have to do some excersises. Well i ran into this problem. I have to determin if a list of int's contains a int that is a power. So i have been trying using "any" with a helper function which return a Bool if the int is a power and applying that to the list. I think that this aproach should work. Anyway the problem im having is in the helper function. I have been trying this: isPower n = let powerF = floor(sqrt(n)) in if (powerF*PowerF == n) then True else False but this doesnt work. Prelude says that the definition of isPower needs a fractional int (if i remember correctly) anyway it looks like something is wrong with my type's? Could you guys help me out? Is there a simple standard function i overlooked. Or what should i change? Thanks in advance guys/gals
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- Hal Daume III | hdaume@isi.edu "Arrest this man, he talks in maths." | www.isi.edu/~hdaume

Also note that if x then True else False is just a verbose way of writing x At 2:51 PM -0800 11/7/03, Hal Daume III wrote:
try "floor (sqrt (fromIntegral n))"
On Fri, 7 Nov 2003 ddekker6@chello.nl wrote:
hello all,
im new to haskell and have to do some excersises. Well i ran into this problem. I have to determin if a list of int's contains a int that is a power. So i have been trying using "any" with a helper function which return a Bool if the int is a power and applying that to the list. I think that this aproach should work. Anyway the problem im having is in the helper function. I have been trying this: isPower n = let powerF = floor(sqrt(n)) in if (powerF*PowerF == n) then True else False
[...] -- ------------------------------------------------------------------ Hamilton Richards, PhD Department of Computer Sciences Senior Lecturer The University of Texas at Austin 512-471-9525 1 University Station C0500 Taylor Hall 5.138 Austin, Texas 78712-1188 ham@cs.utexas.edu hrichrds@swbell.net ------------------------------------------------------------------

Are you looking for numbers that are squares, or any power (probably any power at least two)? If it's the latter you need something more interesting that sqrt. An alternative to squaring the number agagin is to use properFraction. Brandon On Fri, 7 Nov 2003, Hamilton Richards wrote:
Also note that
if x then True else False
is just a verbose way of writing
x
At 2:51 PM -0800 11/7/03, Hal Daume III wrote:
try "floor (sqrt (fromIntegral n))"
On Fri, 7 Nov 2003 ddekker6@chello.nl wrote:
hello all,
im new to haskell and have to do some excersises. Well i ran into this problem. I have to determin if a list of int's contains a int that is a power. So i have been trying using "any" with a helper function which return a Bool if the int is a power and applying that to the list. I think that this aproach should work. Anyway the problem im having is in the helper function. I have been trying this: isPower n = let powerF = floor(sqrt(n)) in if (powerF*PowerF == n) then True else False
[...] -- ------------------------------------------------------------------ Hamilton Richards, PhD Department of Computer Sciences Senior Lecturer The University of Texas at Austin 512-471-9525 1 University Station C0500 Taylor Hall 5.138 Austin, Texas 78712-1188 ham@cs.utexas.edu hrichrds@swbell.net ------------------------------------------------------------------ _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

But note that x `seq` x is equivalent to x, even operationally. To see why denotationally, note that if x evaluates to _|_, so does x `seq` x. And if x evaluates to a value v, so does x `seq` x. To see why operationally, consider the two lists: let x = 1+1 in [x `seq` x] let x = 1+1 in [x] Using conventional lazy evaluation in both cases, the term "1+1" is not evaluated until the head of the list is taken. In other words, x `seq` x in no way hurries the evaluation of x. -Paul Wolfgang Jeltsch wrote:
Am Samstag, 8. November 2003, 00:22 schrieb Hamilton Richards:
Also note that if x then True else False is just a verbose way of writing x
Actually, it's just a verbose way of writing x `seq` x, but this detail is, of course, not interesting for beginners.
Wolfgang

Am Samstag, 8. November 2003, 13:13 schrieb Paul Hudak:
But note that x `seq` x is equivalent to x, even operationally. To see why denotationally, note that if x evaluates to _|_, so does x `seq` x. And if x evaluates to a value v, so does x `seq` x. To see why operationally, consider the two lists:
let x = 1+1 in [x `seq` x] let x = 1+1 in [x]
Using conventional lazy evaluation in both cases, the term "1+1" is not evaluated until the head of the list is taken. In other words, x `seq` x in no way hurries the evaluation of x.
Yes, you are right.
-Paul
Wolfgang

On Saturday 08 November 2003 04:31 am, Wolfgang Jeltsch wrote:
Am Samstag, 8. November 2003, 00:22 schrieb Hamilton Richards:
Also note that
if x then True else False
is just a verbose way of writing
x
Actually, it's just a verbose way of writing x `seq` x, but this detail is, of course, not interesting for beginners.
?? Sorry, but x `seq` x is just a verbose way of saying x. Matt

It seems to me that a neater approach might be to create a list of powers (squares) and see if it has any members in common with the list supplied. Of course you have to deal with the issue of not knowing in advance how many members of the list of squares will be needed... which can lead to using some rather interesting features of Haskell. #g -- At 23:46 07/11/03 +0100, ddekker6@chello.nl wrote:
hello all,
im new to haskell and have to do some excersises. Well i ran into this problem. I have to determin if a list of int's contains a int that is a power. So i have been trying using "any" with a helper function which return a Bool if the int is a power and applying that to the list. I think that this aproach should work. Anyway the problem im having is in the helper function. I have been trying this: isPower n = let powerF = floor(sqrt(n)) in if (powerF*PowerF == n) then True else False but this doesnt work. Prelude says that the definition of isPower needs a fractional int (if i remember correctly) anyway it looks like something is wrong with my type's? Could you guys help me out? Is there a simple standard function i overlooked. Or what should i change? Thanks in advance guys/gals
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
------------ Graham Klyne For email: http://www.ninebynine.org/#Contact

I'm new to haskell and have to do some excersises.
Okay. Thank you for being up-front about it. You got some advice, so I'll just add that
So i have been trying using "any" with a helper function
Yep, this is a good way to do it. You may want to consider "filter" or "map" as well, depending on what you want for result. -kzm -- If I haven't seen further, it is by standing in the footprints of giants
participants (9)
-
Brandon Michael Moore
-
ddekker6@chello.nl
-
Graham Klyne
-
Hal Daume III
-
Hamilton Richards
-
ketil+haskell@ii.uib.no
-
Matt Harden
-
Paul Hudak
-
Wolfgang Jeltsch