Why :sprint always prints a “_”?

前端 未结 3 2064
小鲜肉
小鲜肉 2020-11-30 12:31
Prelude> let a = 3
Prelude> :sprint a
a = _
Prelude> let c = \"ab\"
Prelude> :sprint c
c = _

Why does it always print a _?

3条回答
  •  南方客
    南方客 (楼主)
    2020-11-30 12:57

    Haskell is lazy. It doesn't evaluate things until they are needed.

    The GHCi sprint command (not part of Haskell, just a debugging command of the interpreter) prints the value of an expression without forcing evaluation.

    When you write

    let a = 3
    

    you bind a new name a to the right-hand side expression, but Haskell won't evaluate that thing yet. Therefore, when you sprint it, it prints _ as the value to indicate that the expression has not yet been evaluated.

    Try this:

    let a = 3
    :sprint a -- a has not been evaluated yet
    print a -- forces evaluation of a
    :sprint a -- now a has been evaluated
    

提交回复
热议问题