sicp-lang / sicp

A SICP language for Racket.
GNU Lesser General Public License v3.0
190 stars 24 forks source link

Lists are printed in a strange way after evaluate `(require sicp)` #49

Closed drcxd closed 1 month ago

drcxd commented 1 month ago

In DrRacket, if the language is set to #lang racket/base, then evaluate a list is printed in the following form:

Welcome to DrRacket, version 8.11.1 [cs].
Language: racket/base, with debugging; memory limit: 128 MB.
> (list 1 2 3 4)
'(1 2 3 4)

After evaluating (require sicp), lists are printed in a difficult to read form:

> (require sicp)
> (list 1 2 3 4)
(mcons 1 (mcons 2 (mcons 3 (mcons 4 '()))))

Could lists be printed in the same way before (require sicp) is evaluated? I am asking for this feature because I am using racket in Emacs which can not (or I do not know how to) specify the #lang line, so I have to evaluate (require sicp) whenever there is a dependency to it.

soegaard commented 1 month ago

In Scheme and Racket lists are made of pairs.

The list created by (list 1 2 3) is internally stored as (cons 1 (cons 2 (cons 3 '()))). Here (cons a d) represents a pair of two values a and d.

In Scheme pairs are mutable and in Racket pairs are immutable. In Racket cons means an immutable pair and mcons means a mutable pair. Since the sicp language attempts to work like Scheme, the sicp list produces a list that consists of mutable pairs. This why the normal Racket printer used in Emacs uses mcons to print the list.

Due to the way you started scip (in the repl using (require sicp)), you are actually still using the normal Racket language, but have imported functions from sicp. The REPL thus prints all values as if they are Racket values.

I think, you'll need to make a file and use #lang sicp at the top and run that file to tell the system you are using the sicp language.

drcxd commented 1 month ago

Thanks for the clarification, now I know that it is an issue on the Emacs end.