How to get an infinite list of permissions using the methods below in haskell

I am trying to make an endless list of permissions in the same way as I did them, as shown below, for a list of Fibonacci numbers and factorials.

fibs = 0 : 1 : zipWith (+) fibs (tail fibs) facs = 1 : zipWith (*) [1 ..] facs 

thanks

+4
source share
4 answers

Generating powers of numbers is as simple as using 'iterate':

 iterate (*2) 1 

To find a specific power (instead of listing it), it is faster to use (^) . To see the individual steps of large multiplication, you can use scanl :

 scanl (*) 1 [2, 3, 5, 7] 

Finally, to create a list of all the squares, I recommend this approach:

 fix (\more rs -> s : more (r + 1) (s + 2*r + 1)) 0 0 

Or, if you're not comfortable with fix , here are two alternative versions:

 unfoldr (\(r, s) -> Just (s, (r + 1, s + 2*r + 1))) (0, 0) map snd . iterate (\(r, s) -> (r + 1, s + 2*r + 1)) $ (0, 0) 
+6
source

I think you can define an infinite sequence of squares with just a list comprehension:

 powers = [ ii*ii | ii <- [1 ..]] take 10 powers => [1,4,9,16,25,36,49,64,81,100] 

Edit: It was explained that you are performing authority 2, which can also be done with a list:

 powersOf2 = [ 2^ii | ii <- [0 ..]] take 10 powersOf2 => [1,2,4,8,16,32,64,128,256,512] 

You can extrapolate this to the generator function for any base:

 powersOfN nn = [ nn^ii | ii <- [0 ..]] take 10 (powersOfN 3) => [1,3,9,27,81,243,729,2187,6561,19683] take 10 (powersOfN 17) => [1,17,289,4913,83521,1419857,24137569,410338673,6975757441,118587876497] 
+3
source
 powers n = 1 : map (n*) (powers n) 
+2
source

For better readability, you can also use map :

List of consecutive degrees 2:

 λ> map (2^) [0..10] [1,2,4,8,16,32,64,128,256,512,1024] 

Sequential squares:

 λ> map (^2) [0..10] [0,1,4,9,16,25,36,49,64,81,100] 
+2
source

Source: https://habr.com/ru/post/1446787/


All Articles