Search code examples
haskellghci

In GHCi, can I use the result of the previous expression?


I'm doing some experimenting in GHCi, and I have a moderately long running (5 minutes) operation that I'm trying to tune. The result starts printing out partway through, and I can often tell that my algorithm isn't correctly tuned yet after 1 minute or less, so I cancel the operation. But when I do find the result, I want to allow it continue to the end, then use it afterwards as well. If I assign it when I start it, though, I can't see it as it processes. Is there any way I can access the result of the previous expression entered into GHCi?


Solution

  • GHCi has the special variable it for this purpose.

    Prelude> 1
      1
    Prelude> it
      1
    

    The reason for this, as explained in the GHCi docs is that non-IO expressions behave like so

    someExpr ==> let it = someExpr
                 print it
    

    If someExpr were IO then we'd have

    it <- someExpr
    print it
    

    so it should always be the result of your previous expression.