Why/how does recursive IO work?

前端 未结 2 2029
自闭症患者
自闭症患者 2021-01-17 16:57

Haskell IO is often explained in terms of the entire program being a pure function (main) that returns an IO value (often described as an imperative IO program)

2条回答
  •  青春惊慌失措
    2021-01-17 17:16

    In this case, main is a value of type IO () rather than a function. You can think of it as a sequence of IO a values:

    main = getLine >>= putStrLn >> main
    

    This makes it a recursive value, not unlike infinite lists:

    foo = 1 : 2 : foo
    

    We can return a value like this without needing to evaluate the whole thing. In fact, it's a reasonably common idiom.

    foo will loop forever if you try to use the whole thing. But that's true of main too: unless you use some external method to break out of it, it will never stop looping! But you can start getting elements out of foo, or executing parts of main, without evaluating all of it.

提交回复
热议问题