无法解析模块:意外 = 期望在第 1 列或输入末尾缩进

Unable to parse module: unexpected = expecting indentation at column 1 or end of input

写了这段代码

module Main where

import Prelude
import Data.List (List)
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, log)

type Entry = {
  firstName :: String,
  lastName :: String,
  address :: Address
}

type Address = {
  street :: String,
  city :: String,
  state :: String
}

type AddressBook = List Entry

showEntry :: Entry -> String
showEntry entry = entry.lastName <> ", " <>
                  entry.firstName <> ", " <>
                  showAddress entry.address

showAddress :: Address -> String                  
showAddress address = address.street <> ", " <>
                      address.city <> ", " <> 
                      address.state

main :: forall e. Eff (console :: CONSOLE | e) Unit
main = do
  log "Hello Sailor!"
  address = {street: "123 Fake St.", city: "Faketown", state: "CA"}
  showAddress address

所有内容都缩进两个空格

我收到错误

Error found:
at src/Main.purs line 34, column 11 - line 34, column 11

  Unable to parse module:
  unexpected =
  expecting indentation at column 1 or end of input


See https://github.com/purescript/documentation/blob/master/errors/ErrorParsingModule.md for more information,
or to contribute content related to this error.

我也试过了

main = do
  log "Hello Sailor!"
  address :: Address 
  address = {street: "123 Fake St.", city: "Faketown", state: "CA"}
  showAddress address

但仍然出现相同的错误。

您不能在 do 表示法内或除顶级以外的任何地方有特别的绑定。

如果你想命名一个中间值,你必须使用let:

main = do
  log "Hello Sailor!"
  let address = {street: "123 Fake St.", city: "Faketown", state: "CA"}
  showAddress address

在你的情况下,由于 address 不依赖于前面的代码,你也可以将它与 where:

绑定
main = do
  log "Hello Sailor!"
  showAddress address
  where
      address = {street: "123 Fake St.", city: "Faketown", state: "CA"}

即使在顶层,您也不可能拥有独立的绑定。看到 module Main 之后最顶部的 where 了吗?这意味着此模块内的所有内容都是 where-bound.

所以正确的说法是这样的:绑定永远不能独立存在,它们总是必须是 let- 或 where- 绑定。

另请注意:您可以在单个 let 或单个 where:

中有多个绑定
f x = do
    let y = a + 42
        z = y * b
    pure $ z - 3
    where
         a = x + 1
         b = a * 2