如何记录 Maybe 值?

How to log a Maybe value?

这段代码是我写的(基于 PureScript by Example 中的 AddressBook 示例)

findEntry :: String -> String -> AddressBook -> Maybe Entry
findEntry firstName lastName = head <<< filter filterEntry
  where 
    filterEntry :: Entry -> Boolean
    filterEntry entry = entry.firstName == firstName && entry.lastName == lastName
main :: forall e. Eff (console :: CONSOLE | e) Unit
main = do
  let address1 = {street: "123 Fake St.", city: "Faketown", state: "CA"}
  let address2 = {street: "234 Fake St.", city: "Faketown", state: "CA"}
  let entry1 = {firstName: "foo1", lastName: "bar1", address: address1}
  let entry2 = {firstName: "foo2", lastName: "bar2", address: address2}
  let addressBook = insertEntry entry2 (insertEntry entry1 emptyBook)
  let output = map showEntry (findEntry "foo1" "bar1" addressBook)
  map log (output)
  log "Hello Sailor!"

但是我得到一个错误

Compiling Main
Error found:
in module Main
at src/Main.purs line 53, column 3 - line 53, column 7

  Could not match type

    Eff
      ( console :: CONSOLE
      | t1
      )

  with type

    Maybe


while trying to match type Eff
                             ( console :: CONSOLE
                             | t1
                             )
                             Unit
  with type Maybe t0
while checking that expression log "Hello Sailor!"
  has type Maybe t0
in value declaration main

where t0 is an unknown type
      t1 is an unknown type

编辑::我试过了

let output = map showEntry (findEntry "foo1" "bar1" addressBook)
log output

现在我收到错误

at src/Main.purs line 52, column 7 - line 52, column 13

  Could not match type

    Maybe String

  with type

    String

可以记录字符串,所以你必须将output转换为字符串,然后再传递给log:

log (show output)

或者只是

logShow output