how to do this in haskell ? [x^0,x^1,x^2,x^3 …]

  • A+

i want to have a list like this one

[x^0,x^1,x^2,x^3 ...]  

is it possible to have such a list

for example

 ex : x = 2   [1,2,4,8,16,32 ..]  

You can use iterate or unfoldr to double a number many times. This could be more efficient than computing x^n for each n.

Below, I use x=2, but you can use any x.

> take 10 $ iterate (*2) 1 [1,2,4,8,16,32,64,128,256,512] > take 10 $ unfoldr (/x -> Just (x,2*x)) 1 [1,2,4,8,16,32,64,128,256,512] 

Also beware that bounded integer types such as Int will overflow pretty fast in this way.


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: