Null value in Mit-Scheme?
Asked Answered
M

4

30

Can anyone tell me what is the null value representation in mit-scheme? In the SICP book, it should be "nil" but it doesn't work. Thanks.

Mcgraw answered 2/2, 2012 at 16:11 Comment(0)
L
30

'() should work. Basically, nil in scheme is the empty list, so quoting an empty list gives you nil.

Lucent answered 2/2, 2012 at 16:17 Comment(2)
Yeah. THanks. It works. I tested with (cons 2 (cons 3 (cons 4 ()))) gives (2 3 4).Mcgraw
Omg, this answer saved me so much aggravation. All I could glean from any Scheme docs was () or 'nil and obviously neither can be used to represent an empty list. And probably doesn't help that SICP doesn't introduce quotation until halfway through... Thank you!Notornis
S
13

There is the history. visit http://web.archive.org/web/20070808004043/http://wiki.wordaligned.org/sicp/published/FrequentlyAskedQuestions

Original(broken): http://wiki.wordaligned.org/sicp/published/FrequentlyAskedQuestions


Text from the above FAQ (in case of Internet Archive breakage):

Why isn’t “nil” working?

The quick answer is: nil is no longer part of Scheme, use '() instead. The long answer follows…

Early examples in Chapter 2 use nil as a list terminator, but when these examples are run using (e.g.) MIT Scheme, you get:

;Unbound variable: nil

Similarly, using () or null in place of nil may work on some implementations, but neither is portable. The Wizard Book addresses the issue in this footnote

It’s remarkable how much energy in the standardization of Lisp dialects has been dissipated in arguments that are literally over nothing: Should nil be an ordinary name? Should the value of nil be a symbol? Should it be a list? Should it be a pair? In Scheme, nil is an ordinary name, which we use in this section as a variable whose value is the end-of-list marker (just as true is an ordinary variable that has a true value). Other dialects of Lisp, including Common Lisp, treat nil as a special symbol. The authors of this book, who have endured too many language standardization brawls, would like to avoid the entire issue. Once we have introduced quotation in section 2.3, we will denote the empty list as ‘() and dispense with the variable nil entirely.

Since this was written, nil has been excised from the Scheme standard—but the bottom line holds: use ‘(), not nil. In an email to the accu-sicp list, Mike notes:

It’s a troublesome thing, this nil/null/'() business. Scheme48 and scm don't define null, and guile defines it but as a procedure akin to common lisp's null (to go with its nil which behaves like cl's nil, which itself is distinct from '()—maybe this had something to do with fsf's plans to re-do emacs in guile). I think the best bet is to replace the authors’ use of nil with ‘().

[Copied with just a touch of typographical clean up, to better match Markdown and usual ASCII input into the languages/implementations in question.]

Stimulative answered 20/3, 2012 at 11:51 Comment(0)
R
3

I use MIT/GNU Scheme microcode 15.3, () and '() all works. (as you said, nil and null doesn't work).

1 ]=> ()

;Value: ()

1 ]=> '()

;Value: ()

1 ]=> (cons 1 ())

;Value 2: (1)

1 ]=> (cons 1 '())

;Value 3: (1)
Ronnyronsard answered 11/10, 2014 at 15:33 Comment(0)
L
0

(list ) '() and () can represent null. for example,

(define (transpose mat) (accumulate-n cons () mat))

or substitute () with '() or (list ).

Liana answered 1/4, 2014 at 18:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.