Emacs: turn off the pretty print version in racket mode

I am running Emacs 24.5.1 on Windows 10 and working through SICP . The MIT Edwin editor does not work well, especially on Windows. A racket seems like a good alternative. I installed both Racket and racket-mode , and everything seems to be working fine. However, racket-mode insists on printing my results. How to get it to print in decimal?

For instance,

 (require sicp) (define (square x) (* xx)) (define (average xy) (/ (+ xy) 2)) (define (improve guess x) (average guess (/ x guess))) (define (good-enough? guess x) (< (abs (- (square guess) x)) 0.001)) (define (sqrt-iter guess x) (if (good-enough? guess x) guess (sqrt-iter (improve guess x) x))) 

This gives results such as

 > (sqrt-iter 1 2) 577/408 

A lot of documentation appears when I google the terms "Racket" and "pretty-print", but I was unlucky in realizing it. The Racket documentation seems to control pretty-printed information with some variable starting with "pretty-print". However, nothing starts with racket- or pretty inside Mx . Perhaps the shape of the fraction is not what Racket considers pretty-printed?

+5
source share
3 answers

Run an iteration with floating point numbers 1.0 and 2.0, rather than the exact numbers 1 and 2.

Literal 1 is read as an exact integer, while 1.0 or 1. is read as a floating point number.

The / function now works on both exact inaccurate numbers. If you enter the exact numbers, it produces a fraction (which ultimately ends up printing in repl).

That is, you do not see the effect of a pretty printer, but the actual result. The algorithm works effectively only with floating point numbers as input, so you might consider adding a call to exact->inexact to your function.

+3
source

This is actually intentional and is part of the Scheme standard (R5RS, R7RS). It is not limited to Racket, but should be the result of any Scheme / REPL interpreter. This has nothing to do with a beautiful print. Basically this is considered a good thing, as it gives the exact number (rational number), not the floating point approximation. If you want to get a floating point result, request it using 1.0, not 1, etc.

 > (/ 1.0 3) 0.3333333333333333 

Alternatively, you can use the exact->inexact function exact->inexact for example:

 > (exact->inexact 1/3) 0.3333333333333333 
+1
source

As the other answers explain, it turned out that this is actually not a very beautiful print.

However, to answer your question literally (if you ever wanted to disable beautiful print in racket mode):

Variable Emacs racket-pretty-print .

You can view the documentation for this using Ch v .

To change it, you can:

  • Use the Emacs Mx customize user interface.

  • Use (setq racket-pretty-print nil) in the Emacs initialization file, for example, in racket-repl-mode-hook .

+1
source

Source: https://habr.com/ru/post/1260791/


All Articles