Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change output printing style from Racket REPL

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?

like image 713
crowding Avatar asked Sep 10 '14 21:09

crowding


1 Answers

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.

like image 60
Greg Hendershott Avatar answered Oct 22 '22 20:10

Greg Hendershott