title: What can be a minimal example of game written in Haskell?
body: <p><strong>Update after three months</strong></p>
  
  <p>I have an answer below using <code>netwire-5.0.1</code> + <code>sdl</code>, in a structure of Functional Reactive Programming using Arrows and Kleisli Arrows for I/O. Though too simple to be called a "game", it should be very composible and very extendable.</p>
  
  <p><strong>Original</strong> </p>
  
  <p>I am just learning Haskell, and trying to make a small game out of it. However, I would like to see what structure a small(canonical) text game can be. I also try to keep the code as pure as possible. I am now struggling to understand how to implement:</p>
  
  <ol>
  <li>The main loop. There is an example here <a href="https://stackoverflow.com/questions/19285691/how-do-i-write-a-game-loop-in-haskell">How do I write a game loop in Haskell?</a> but it seems that the accepted answer is not tail recursive. I am not exactly sure whether this matters. In my understanding, the memory usage will grow, right?</li>
  <li>State transition. I think this is quite related to the first one, though. I tried a bit using <code>State</code>, and something in <a href="http://www.gamedev.net/page/resources/_/technical/game-programming/haskell-game-object-design-or-how-functions-can-get-you-apples-r3204" rel="nofollow noreferrer">http://www.gamedev.net/page/resources/_/technical/game-programming/haskell-game-object-design-or-how-functions-can-get-you-apples-r3204</a> , but although individual components may work and update in finite steps, I don't see how it can be used in an infinite loop.</li>
  </ol>
  
  <p>If possible, I would like to see a minimal example which basically:</p>
  
  <ol>
  <li>Asks the player to input something, repeatedly</li>
  <li>When some condition is met, change state</li>
  <li>When some other contition is met, exit</li>
  <li>Theoretically can run for infinite time without blowing the memory</li>
  </ol>
  
  <p>I don't have any postable code because I cannot get the very basic stuff. Any other material/examples I found on the web either use some other libraries, like <code>SDL</code> or <code>GTK</code> to drive events. The only one written totally in Haskell I found is <a href="http://jpmoresmau.blogspot.com/2006/11/my-first-haskell-adventure-game.html" rel="nofollow noreferrer">http://jpmoresmau.blogspot.com/2006/11/my-first-haskell-adventure-game.html</a> , but that one does not look like a tail recursion in its main loop too(Again, I don't know if it matters).</p>
  
  <p>Or, probably Haskell is not intended to do things like this? Or probably I should put the <code>main</code> in C?</p>
  
  <p><em>Edit 1</em></p>
  
  <p>So I modified a small example in <a href="https://wiki.haskell.org/Simple_StateT_use" rel="nofollow noreferrer">https://wiki.haskell.org/Simple_StateT_use</a> and made it even simpler(and it does not meet my criteria):</p>
  
  <pre><code>module Main where
  import Control.Monad.State
  
  main = do 
    putStrLn "I'm thinking of a number between 1 and 100, can you guess it?"
    guesses &lt;- execStateT (guessSession answer) 0
    putStrLn $ "Success in " ++ (show guesses) ++ " tries."
    where
      answer = 10
  
  guessSession :: Int -&gt; StateT Int IO ()
  guessSession answer =
      do gs &lt;- lift getLine    -- get guess from user
         let g = read gs       -- convert to number
         modify (+1)           -- increment number of guesses
         case g of
           10 -&gt; do lift $ putStrLn "Right"
           _ -&gt; do lift $ putStrLn "Continue"
                   guessSession answer
  </code></pre>
  
  <p>However, it will eventually overflow the memory. I tested with</p>
  
  <pre><code>bash prompt$ yes 1 | ./Test-Game
  </code></pre>
  
  <p>and the memory usage starts growing linearly.</p>
  
  <p><em>Edit 2</em></p>
  
  <p>OK, I found <a href="https://stackoverflow.com/questions/13782222/haskell-recursion-and-memory-usage">Haskell recursion and memory usage</a> and gained some understanding about the "stack"... So is there anything wrong about my testing method?</p>