title: Preventing endless loops in Yampa/Animas with SF's depending on each other
body: <p>I'm trying to understand how this functional reactive programming works, and I have run into a problem. I am trying to create a <a href="http://www.youtube.com/watch?v=S1SzjsP34PE" rel="noreferrer">boid simulation</a>, but I'm starting out slowly, and I have for now defined a boid as a function taking a starting position and creating a signal function from a position to a position, where the input is the point it is moving towards and the output is the current position:</p>
  
  <pre><code>type Position2 = Vector2 Float
  
  boid :: Position2 -&gt; SF Position2 Position2
  boid s = loop (arr (uncurry seek) &gt;&gt;&gt; integral &gt;&gt;&gt; arr (^+^ s) &gt;&gt;&gt; arr dup)
  </code></pre>
  
  <p>the seek function takes two inputs (because of the loop). The current position and the target position. Then it just creates a vector pointing from the current position towards the target position with a magnitude of 50 i.e. the velocity. Then the velocity is integrated and the starting position is added. In the end the signal is split into two, so one can become the output and the other can loop back into the seek function.</p>
  
  <p>Now i can define boids like this:</p>
  
  <pre><code>aBoid = constant (vector2 500 500) &gt;&gt;&gt; (boid (vector2 600 60))
  bBoid = aBoid &gt;&gt;&gt; (boid (vector2 3 4))
  </code></pre>
  
  <p>Here aBoid seeks towards the point (500, 500) and bBoid seeks towards aBoid.</p>
  
  <p>My problem is, when i want the two boids to seek toward each other. When i do this:</p>
  
  <pre><code>aBoid = bBoid &gt;&gt;&gt; (boid (vector2 600 60))
  bBoid = aBoid &gt;&gt;&gt; (boid (vector2 3 4))
  </code></pre>
  
  <p>The program just prints: <code>ProgramName: &lt;&lt;loop&gt;&gt;</code> which I assume means that it goes into an endless loop.</p>
  
  <p>I have also tried to use the <code>par</code> function like this:</p>
  
  <pre><code>sim :: SF a [Position2]
  sim = loop (arr snd &gt;&gt;&gt; par route [boid (vector2 10 10), boid (vector2 100 100)] &gt;&gt;&gt; arr dup)
  </code></pre>
  
  <p>here the <code>route</code> function just maps the output of each boid to the input of another one (like <code>zip</code>, but with an offset of 1)</p>
  
  <p>This also gives the <code>&lt;&lt;loop&gt;&gt;</code> message.</p>
  
  <p>I would think that having objects depend on the state of each other should be a pretty common problem when dealing with reactive systems, so I hope there is some elegant solution.</p>
  
  <p>I should add that I find this FRP thing very hard and often confusing, so I'm not really sure if i even make sense at all ;)</p>