无法解析模块:unexpected =期望第1列或输入结束时出现缩进

时间:2018-02-13 19:16:06

标签: purescript

写了这段代码

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

但仍然会遇到同样的错误。

1 个答案:

答案 0 :(得分:2)

您无法在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"}

即使在顶层,你也无法拥有自己的绑定。在where之后的最顶部看到module Main?这意味着此模块中的所有内容都是where - 绑定。

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

另请注意:您可以在一个let或一个where中包含多个绑定:

f x = do
    let y = a + 42
        z = y * b
    pure $ z - 3
    where
         a = x + 1
         b = a * 2