编写此代码
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
-bound。
还注意:您可以在单个let
或单个where
中具有多个绑定:
f x = do
let y = a + 42
z = y * b
pure $ z - 3
where
a = x + 1
b = a * 2