Search code examples
netlogofractals

“Expecting a constant value” error when constructing a NetLogo list


I am currently working to create a pictorial representation of the Levy C-Curve in NetLogo using an IFS construction scheme. I have found two functions which describe how to iteratively map the locations of two turtles and should result in the desired curve after thousands of iterations. Here is my code so far:

 ;;;;;; some useful complex operations

 ; to take re(z) of a complex number a + bi inputed as the list of coordinates [a b]

 to-report re [z]
 report first z
 end


 ; to take im(z)

 to-report im [z]
 report last z
 end

 ; to multiply two complex numbers

 to-report complex_mul [z1 z2]
 report list (re z1 * re z2 - im z1 * im z2) 
             (re z1 * im z2 + im z1 * re z2) 
 end


 ; to add complex numbers

 to-report complex_add [z1 z2]
 report list (re z1 + re z2)
             (im z1 + im z2)
 end


 ; to dilate complex numbers by a scalar fraction

 to-report complex/real [z1 real]
 report list (re z1 / real)
             (im z1 / real)
 end 

 ; to initialize

 to setup
 ca
 setup-turtles
 reset-ticks
 end


 ; create 2 turtles located at the initial set of points {0, 1}

 to setup-turtles
 crt 2
 ask turtle 0 [ setxy 0 0]
 ask turtle 1 [ setxy 1 0]
 ask turtles [ pd]
 end


 ; to create the first function to transform the turtle's location

 to-report nextz_1 [z] 
 report complex/real (complex_mul [1 -1] z) 2
 end 


 ; to create the second function to transform the turtle's location

 to-report nextz_2 [z]
 report complex_add [1 0] 
                    (complex/real (complex_mul [1 1] 
                                                (complex_add z [-1 0]))
                                   2) 
 end

 ; finally we are creating the Levy Curve

 to levy
  ask turtles [ run one-of (list task setxy re (nextz_1 [xcor ycor]) im (nextz_1 [xcor ycor])
                                 task setxy re (nextz_2 [xcor ycor]) im (nextz_2 [xcor ycor])
                               )
           ]

 end

However, I'm receiving an error message in my "levy" code block where I call re (nextz_1 [xcor ycor]) etc., saying that NetLogo is expecting a constant value in place of xcor and ycor.

How would I fix this issue?


Solution

  • At http://ccl.northwestern.edu/netlogo/docs/faq.html#listexpectedconstant the NetLogo FAQ says:

    If a list contains only constants, you can write it down just by putting square brackets around it, like [1 2 3].

    If you want your list to contain items that may vary at runtime, the list cannot be written down directly. Instead, you build it using the list primitive.

    You actually got this right in some other places in your code, but in the levy procedure, you need to replace e.g. [xcor ycor] with list xcor ycor.

    To NetLogo, [xcor ycor] looks like a reporter block, not like a list.