4

For eg. How do I print out something like this:

showEntry entry = entry.lastName ++ "\t" ++
                  entry.firstName ++ "\t" ++
                  entry.phone
print(showEntry {lastName: 'Doe', firstName: 'John', phone: '555-555-5555'})

This just prints out Doe\tJohn\t555-555-5555.

  • 1
    How are you running this function? If you are using `psci`, the `Show` instance will escape the tab character, which is why you see `\t` in the output. – Phil Freeman Dec 01 '14 at 02:17
  • Yes, I am using `psci`. That explains it. I think you mention that in the book somewhere. Can I make sure the "\t" is not escaped during print, like preventing the Show instance's standard behavior? Or by using a different function altogether instead of `print`? – Arunan Rabindran Dec 01 '14 at 13:47
  • The call to `print` is baked into `psci`. You could hack in a call to `console.log` with an unsafe FFI primitive: `foreign import unsafeTracing "function tracing(x) { console.log(x); return x; }" :: forall a. a -> a` – Phil Freeman Dec 02 '14 at 18:53

1 Answers1

1

The question was based on an old version of the language and associated tools. Nowadays, this is what you can do.

Use log from purescript-console (https://pursuit.purescript.org/packages/purescript-console/4.2.0/docs/Effect.Console#v:log).

> import Effect.Console
> log "Hello\tSailor!"
Hello   Sailor
unit

>

The REPL (purs repl) uses show implicitly to encode values as strings. To get around this, one can use the log effect (as Phil Freeman mentioned in his comment, though there is nothing unsafe about using log).

erisco
  • 14,154
  • 2
  • 40
  • 45