Default output syntax is `print`, should be `output`
assertivist opened this issue · 4 comments
SICP exercise 2.32 reads
We can represent a set as a list of distinct elements, and we can represent the set of all subsets of the set as a list of lists. For example, if the set is (1 2 3), then the set of all subsets is (() (3) (2) (2 3) (1) (1 3) (1 2) (1 2 3)). Complete the following definition of a procedure that generates the set of subsets of a set and give a clear explanation of why it works:
(define (subsets s) (if (null? s) (list nil) (let ((rest (subsets (cdr s)))) (append rest (map <??> rest)))))
My solution is this:
(define (subsets s)
(if (null? s)
(list nil)
(let ((rest (subsets (cdr s))))
(append rest (map (lambda (ss) (cons (car s) ss)) rest)))))
which totally works, but since the default output syntax is print, I get this in the REPL:
> (subsets (list 1 2 3))
(mcons
'()
(mcons
(mcons 3 '())
(mcons
(mcons 2 '())
(mcons
(mcons 2 (mcons 3 '()))
(mcons
(mcons 1 '())
(mcons (mcons 1 (mcons 3 '())) (mcons (mcons 1 (mcons 2 '())) (mcons (mcons 1 (mcons 2 (mcons 3 '()))) '()))))))))
>
and I don't think this is very noob friendly. I expected something more like the write output provides:
> (subsets (list 1 2 3))
{() {3} {2} {2 3} {1} {1 3} {1 2} {1 2 3}}
Which still isn't exactly the way it shows list results in the book, but it's still way more legible. Forgive me if this isn't up to the language pack to decide, it seems to me that when I do #lang sicp it should automatically set this output format--I am not totally sure how DrRacket works, so this may not be possible.
In any case, thank you for an excellent method of following along with SICP!
You are right, that mcons in the output isn't that user friendly.
What does the output look like in the book?
The book just shows parens anywhere there are brackets in the above. It's not a huge deal, as the output looks the same beyond that.
@wangjiezhe I tested the commit above, everything seems to work, but I'm unable to determine whether or not it set write instead of print; the "language picker" window that I see those options in (using DrRacket, screenshot attached) doesn't have the "sicp" lang listed. Let me know if you have/come up with a reproduction procedure for this bug/fix.

I know I could probably reinstall DrRacket and then try, but I'd rather not.
I just choose Determine language from source, and do not change any detail settings, which is print on my computer. But I don't find any differences between these two options.
BTW, if I chose write with the original code, it would show curly brackets instead of 'mcons'.