简体   繁体   中英

How to print the folowing to console in scheme r5rs language

(define x '())
x

in repl console gives => '()

(define x '())
(display x)

gives => () but I want to print it like '()

How can make display print '() rather than () ?

I need this because my function is logging the error incase of bad inputs provided by user and i want to print '() input as '() only, kinda like how scheme does when you run the following:

(define x '())
(cdr x)

gives =>

mcdr: contract violation
  expected: mpair?
  given: '() (Note the ')

The code:

(define x '())
(define y '(1 . 2))
x ; ==> ()
y ; ==> (1 . 2)

Thus evaluating x in a Scheme REPL will show () since when you evaluate '() it evaluates to the thing without the first ' .

Racket oddeties

In Racket they have configurable how the REPL is supposed to print values in the REPL / interactive window. In #lang racket when you use display you'll see what the value really is

(display x) ; prints ()
(display y) ; prints (1 . 2)

However in $lang r5rs the default REPL with default settings output setting is print :

x ; ==> '()
y ; ==> (mcons 1 2)

With constructor as output style:

x ; ==> empty
y ; ==> (cons 1 2)

With quasiquote as output style:

x ; ==> `()
y ; ==> `(1 . 2)

All of the above doesn't really print the value. It prints an expression in the chosen style that, when evaluated, will become the same value. '() , empty , and `() all evaluate to () so all of them are printed for the value you get when evaluating '()

The only sensible choice is to use write as output style. This will print the real value in the REPL in the same manner as all other scheme implementations:

x ; ==> ()
y ; ==> (1 . 2)
 (format "'%s" '())

scheme ia中的输出语法和输入语法不一样,需要自己创建。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM