title: Display the terms of a sequence as they are computed
body: <p>I wrote a little program that displays, in a window, the first 30 terms of the Fibonacci sequence. However, it does not work. My program (see below) computes all the terms, but only the last term is displayed. I would like to display all 30 terms, as soon as they are computed. What should I do?</p>
  
  <pre><code>module Main where
  
  import           Control.Monad         (forM)
  import           Graphics.UI.Gtk
  import           Graphics.UI.Gtk.Glade
  
  fib :: Int -&gt; Integer
  fib 1 = 1
  fib 2 = 1
  fib n = fib(n-1)+fib(n-2)
  
  data GUI = GUI {
    mainWin :: Window,
    clickMe :: Button,
    display :: Label
    }
  
  loadGlade :: IO GUI
  loadGlade = do
    Just xml &lt;- xmlNew "gladeFile.glade"
    mw &lt;- xmlGetWidget xml castToWindow "wdwFirst"
    bc &lt;- xmlGetWidget xml castToButton "btnClick"
    ld &lt;- xmlGetWidget xml castToLabel "lblDisplay"
  
    return $ GUI mw bc ld
  
  connectGui :: GUI -&gt; IO  (ConnectId Button)
  connectGui gui = do
    onDestroy (mainWin gui) mainQuit
    onClicked (clickMe gui) (guiAnswer gui)
  
  guiAnswer :: GUI -&gt; IO()
  guiAnswer gui = do
    a&lt;-forM [1..30] (\t -&gt; labelSetText (display gui) (show $ fib t))
    --labelSetText (display gui) "WELCOME!!"
    putStr ""
  
  
  main :: IO ()
  main = do
    initGUI
    gui &lt;- loadGlade
    connectGui gui
    widgetShowAll (mainWin gui)
    mainGUI
  </code></pre>
  
  <p>EDIT: </p>
  
  <p>for postGuiAsync, I tried this:</p>
  
  <pre><code>guiAnswer :: GUI -&gt; IO()
  guiAnswer gui = do
    a&lt;-forM [1..40] (\t -&gt; postGUIAsync $ labelSetText (display gui) (show $ fib t))
    --labelSetText (display gui) "WELCOME!!"
    --mainContextIteration mainContextDefault False
    print ""
  </code></pre>
  
  <p>but without success (it still displays only the final number), and I'm not sure about my code.</p>
  
  <p>EDIT2:
  I tried this :</p>
  
  <pre><code>guiAnswer :: GUI -&gt; IO()
  guiAnswer gui = do
    forkIO $ forM_ [1..40] (\t -&gt; postGUIAsync $ labelSetText (display gui) (show $ fib t))
  </code></pre>
  
  <p>but it is not correct.Can you help me?</p>
  
  <p>the error is :</p>
  
  <pre><code>Couldn't match type `GHC.Conc.Sync.ThreadId' with `()'
   Expected type: IO ()
     Actual type: IO GHC.Conc.Sync.ThreadId
   In a stmt of a 'do' block:
     forkIO
     $ forM_
         [1 .. 40]
         (\ t -&gt; postGUIAsync $ labelSetText (display gui) (show $ fib t))
   In the expression:
     do { forkIO
          $ forM_
              [1 .. 40]
              (\ t -&gt; postGUIAsync $ labelSetText (display gui) (show $ fib t)) }
  </code></pre>