34
votes

I'm trying with SICP and I got some code. So I started with:

#lang scheme
(word 'comp 'uter)

Returned error: Function (word) undefined.

Even if I tried to copy this into IDE(Run):

(define word?
  (let ((number? number?)
        (symbol? symbol?)
        (string? string?))
    (lambda (x)
      (or (symbol? x) (number? x) (string? x)))))

Still the same.

I think it may be certain problem with version of language or else.


Above are from "Simply Scheme" and when I introduce code exactly in SICP:

(define (sqrt x)
    (sqrt-iter 1.0 x))

IDE returned sqrt-iter undefined. The code can be found in chapter one: http://mitpress.mit.edu/sicp/code/index.html

3
Can you link to the part of SICP you're working on?C. Warren Dale
Wait a minute, you are trying to run the function word but defined it as word?. You would expect it not to be found...Justin Ethier
See this chapter in "Simply Scheme":cs.berkeley.edu/~bh/ssch1/showing.html I got the code above.user2871676
@cloudr3414 The link you gave does not include the code above. Also, the book that that's an except from is not SICP. The SICP book is available in its entirety from mitpress.mit.edu/sicp/full-text/book/book.html .Joshua Taylor
sqrt-iter should be undefined if you haven't defined it either.C. Warren Dale

3 Answers

56
votes

In DrRacket there is a SICP compatibility language

1. From the Package Manager

In the documentation there is an easy guide to how it's installed from DrRacket:

  1. Open the Package Manager: in DrRacket choose the menu "File" then choose "Package Manager...".

  2. In the tab "Do What I Mean" find the text field and enter: "sicp"

  3. Click the "Install" button. This produces lots of output. Don't worry about it even when there are warnings.

  4. Test it. Make sure DrRacket has "Determine language from source" in the bottom left corner. Write the following program and click RUN:

    #lang sicp 
    
    (inc 42) 
    ; ==> 43
    

Here is a more advanced test that uses the picture language, which needs to be included with #%require:

#lang sicp
(#%require sicp-pict)

;; paint-hires / paint-hi-res renamed to just paint
(paint (below (beside diagonal-shading
                      (rotate90 diagonal-shading))
              (beside (rotate270 diagonal-shading)
                      (rotate180 diagonal-shading))))

Click RUN and you should see a square in the interactions window that gets brighter towards the center.

2 Command line installation

Alternatively, you can also do step 1-3 from a terminal/shell by running the following:

raco pkg install sicp

From here you do step 4. in the first installation instruction to test it.

3. Older versions or DrRacket using planet if the raco pkg didn't work

In DrRacket there is also an old version of SICP compatibility language. While having bottom left select box at "Determine language from source" You may just add:

#lang planet neil/sicp

as the only line in the definitions (top text area) and press RUN and it will be installed. Restart DrRacket and you'll find it available in the language drop down. Good luck. You might get lots of error messages in red. Just ignore it and restart DrRacket. You might not find the choice in the language menu anymore, but by starting every file with #lang planet neil/sicp it still works as a module language.

Judging from the errors, it seems to relate to the picture language module. I tested this sniplet and it still works:

(paint-hires  (below (beside diagonal-shading
                             (rotate90 diagonal-shading))
                     (beside (rotate270 diagonal-shading)
                             (rotate180 diagonal-shading))))
18
votes

Sylwester's answer was what I wanted. However, I have noticed that Racket 6.5 has added direct support for SICP. I think people may want to know that.

Now one can write code like the following in Racket after SICP support is added:

#lang sicp
(#%require sicp-pict)
(paint einstein)
4
votes

[Nice start; keep going, you'll enjoy Scheme!]

In Scheme, programs are developed in an environment. The environment defines a mapping from identifiers to values. Some of the values are functions, some are numbers, etc. When you define a function:

(define (sqrt x)
  (sqrt-iter 1.0 x))

the identifier x is bound as an argument to sqrt, the value 1.0 is a number, and the identifier sqrt-iter is coming from the environment.

A question to ask yourself is "where is sqrt-iter defined; what is it bound to?" Since it is not defined by you, sqrt-iter must come from an environment built into your Scheme or imported into your Scheme. You've not imported anything and sqrt-iter is not defined in Scheme (see resources for R5RS or others). Thus sqrt-iter is unbound.

The same logic applies to every identifier including your use of word.

In your implementation of word? the syntactic-keyword let is used to introduce new bound identifiers. When you write (number? number?) you are introducing a new identifier number? (on the left) and binding it to number? (on the right) coming from the environment (it is defined in Scheme). Using let for this isn't really buying you anything. Your code for word? could be implemented as:

(define (word? x)
  (or (symbol? x) (number? x) (string? x)))   ;; number?