Rahul R Badenkal
Rahul R Badenkal

Reputation: 81

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 ')

Upvotes: 0

Views: 470

Answers (1)

Sylwester
Sylwester

Reputation: 48745

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)

Upvotes: 1

Related Questions