Change output printing style from Racket REPL
Asked Answered
G

1

5

I'm doing problems from SICP, using the #lang planet/neil directive in Racket. I'd prefer to write my code in Emacs, and I'm using Geiser-mode to run a Racket REPL through Emacs.

The way racket prints results tends to use a lot of mcons which makes the results hard to read.

[email protected]> (list 1 2 3 4)
(mcons 1 (mcons 2 (mcons 3 (mcons 4 '()))))    

According to this other question, the output style can be changed inside DrRacket by selecting the "write" output style in the Choose Language dialog box. However this requires the DrRacket GUI; is there a way to change this setting for the Racket REPL?

Geaghan answered 10/9, 2014 at 21:44 Comment(0)
S
9

Background: Unlike SICP style Scheme, Racket lists are immutable. To get mutable lists, in Racket you use mlist. What #lang planet/neil/SICP does (I'm guesing) is (require mpair) and then rename mlist to list. So when you write list in that #lang, you're actually using mlist.

Anyway, mlists print differently, by default. But you can change two parameters.

print-as-expression

(print-as-expression #f)

Now it will print as

{1 2 3 4}

The curly braces instead of parentheses indicate that it's a mutable list. To tweak that, set another parameter:

print-mpair-curly-braces

(print-mpair-curly-braces #f)

And now it should print as:

(1 2 3 4)

To have the plain Racket REPL always do this, I think you could put these two expressions in your Racket init file, e.g. ~/.racketrc on OSX and Linux. Although I'm not sure if the REPL provided by Geiser read the init file, if you eval those expressions once they should persist for a Geiser REPL session, so you could put them in some .rkt file and visit it once.

Shamrao answered 11/9, 2014 at 2:2 Comment(3)
Thanks! One thing to add, these commands aren't accessible when the REPL is inside SICP, but I can type ,enter #f or press C-c C-m in geiser to get back to top level.Geaghan
Ah, OK, so #lang SICP doesn't provide them. I think you can use (require (only-in racket/base print-as-expression print-mpair-curly-braces)) to make them available.Shamrao
As a sidenote, you can, rather easily, make Geiser read the ~/.racketrc file by placing the following in your geiser-racket-init-file (which is located at ~/.racket-geiser by default): (load (find-system-path 'init-file))Businesslike

© 2022 - 2024 — McMap. All rights reserved.