user1712985
user1712985

Reputation: 13

Function imposed on a list

I'm trying to write a Haskell function that takes makes use of this function:

E(x,y)(i,j) = ((i*i) - (j*j) + x, (2*i*j) + y)

The list F(x,y) of a point (x,y) should be an infinite list of items:

F(x,y) = {(0,0) , F(x,y)(0,0), F(x,y)(F(x,y)(0,0)), F(x,y)(F(x,y)(F(x,y)(0,0))), and so forth}

From my understanding, the nth entry of a list F(x,y) is the E(x,y) function composed with itself n times and then applied to (0,0)

This is what I'm thinking so far:

entry :: (Int,Int) -> [(Int,Int)]
efunction (i,j)(x,y) =  ((i*i) - (j*j) + x, (2*i*j) + y)
entry (x,y) = efunction(0,0)(x,y) where
    efunction = (0,0) : iterate efunction(i,j)

Also, (x,y)=(0,0) and remains static. The only variables that change are (i,j).

A sample output would be

entry(1,1) = 
0,0
1,1
1,3
-7,7
1,-97

I'm pretty new to Haskell so I'm been trying to wrap my head around why this doesn't work and how to actually make it work. Any help?

Upvotes: 1

Views: 132

Answers (1)

Daniel Fischer
Daniel Fischer

Reputation: 183873

You're on a good way, using iterate is the right thing. You have a function of two arguments, and want to iterate it with one of the arguments fixed. It's more convenient to use in iterate if the fixed argument is the first, so let's define

step (x,y) (i,j) = (i*i - j*j + x, 2*i*j + y)

Then you get your desired list by iterating the partially applied function step (x,y) with an initial point of (0,0),

entry (x,y) = iterate (step (x,y)) (0,0)

It may be more easy to follow if you define a local function using the argument of entry instead of the partially applied step,

entry (x,y) = iterate next (0,0)
  where
    next (u,v) = (u*u - v*v + x, 2*u*v + y)

Have fun producing your Mandelbrot set ;)

Upvotes: 7

Related Questions