문제

I'd appreciate if someone could point to docs on what "let" does in GHCi, or failing that, explain it convincingly.

So far as I can tell, "let" (without "in") is not part of the Haskell language per se, and on the other hand, it doesn't appear to be a GHCi command either, as it's not prefixed by colon.

도움이 되었습니까?

해결책

While programming in GHCi, you're like programming in the IO monad with do syntax, so for example you can directly execute an IO action, or use monadic bind syntax like r <- someIOFun.

let is also a part of do so you can also use this. I think it's being desugared into let .. in <rest of the computation>, so for example when you do this:

ghci> let a = 1
ghci> someFun
ghci> someFun2

It's like:

let a = 1 in
do someFun
   someFun2

다른 팁

Here's the relevant part of the documentation.

GHCI statements are executed as IO computation. So let is same as what you have inside an IO monad where you bind non-IO expression using let.

For more down-to-the-code details, this comment in TcRnDriver.lhs might be enlighting:

--------------------------------------------------------------------------
                Typechecking Stmts in GHCi

Here is the grand plan, implemented in tcUserStmt

        What you type The IO [HValue] that hscStmt returns
        ------------- ------------------------------------
        let pat = expr ==> let pat = expr in return [coerce HVal x, coerce HVal y, ...]
                                        bindings: [x,y,...]

        pat <- expr ==> expr >>= \ pat -> return [coerce HVal x, coerce HVal y, ...]
                                        bindings: [x,y,...]

        expr (of IO type) ==> expr >>= \ it -> return [coerce HVal it]
          [NB: result not printed] bindings: [it]

        expr (of non-IO type, ==> let it = expr in print it >> return [coerce HVal it]
          result showable) bindings: [it]

        expr (of non-IO type,
          result not showable) ==> error

So a command at the GHCi prompt can have up to three effects: Some code is evaluated, some things are printed, and some variable names are bound. Your case (the first one in the comment) binds variables, but does not print.

The syntax does resemble the do-notation, so @sinan’s answer is somewhat right, but it is not really what is happening under the hood – otherwise, for example, nothing would ever be printed.

GHCI commands are executed in the IO monad and uses do syntax, so the desugaring rules apply. From Real World Haskell

doNotation4 =
    do let val1 = expr1
           val2 = expr2
           {- ... etc. -}
           valN = exprN
       act1
       act2
       {- ... etc. -}
       actN

translates to:

translated4 =
    let val1 = expr1
        val2 = expr2
        {- ... etc. -}
        valN = exprN
    in do act1
          act2
          {- ... etc. -}
          actN
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top