J Atkin
J Atkin

Reputation: 3140

How do I prevent GHCI from evaluating an infinite stream?

Sometimes when using GHCI I accidentally try to evaluate an infinite stream (e.g. [1..]). It keeps printing... and printing... and printing... and printing... So the problem is that the only way to stop the printing is to quit the console, losing all the stuff that I've already done in that session. Is there a way to make GHCI take only the first x items from a list by default?

Upvotes: 1

Views: 111

Answers (1)

Bakuriu
Bakuriu

Reputation: 101929

Simply use Ctrl+C to interrupt the process:

Prelude > let x = 1
Prelude > [1..]
lots of stuff... Interrupted.
Prelude > print x
1

Other than this I believe you cannot really do anything about it except starting writing your expressions with take to be sure to have a finite output... In the end lists are only one of possible infinite outputs and the ghci cannot reliably know what requires shortening and what not. He simply shows the result and output it.

Upvotes: 2

Related Questions