35

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

Mulan
  • 129,518
  • 31
  • 228
  • 259
  • 1
    Can you link to the part of SICP you're working on? – C. Warren Dale Oct 23 '13 at 15:40
  • 3
    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 Oct 23 '13 at 15:43
  • See this chapter in "Simply Scheme":http://www.cs.berkeley.edu/~bh/ssch1/showing.html I got the code above. –  Oct 23 '13 at 15:46
  • @cloudr3414 But those are _different_ functions. Just like there's a function `list` for _constructing_ a list and a function `list?` for checking whether something _is_ a list. – Joshua Taylor Oct 23 '13 at 15:55
  • 1
    @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 http://mitpress.mit.edu/sicp/full-text/book/book.html . – Joshua Taylor Oct 23 '13 at 15:58
  • @Joshua Taylor But the function `word` and `accumulate` should be embedded in `#lang scheme`, if I didn't get _this_ wrong. –  Oct 23 '13 at 16:01
  • @Joshua Thank you, I know it's not exactly the book. But I can't run the code either.. –  Oct 23 '13 at 16:05
  • @cloudr3414 But in the title and question you _specifically_ asked about _SICP_. Is there any reason to assume that the code in the _Simply Scheme_ book that you _are_ reading would run under an one based on SICP? (There may well be, but it's not been included in this question.) – Joshua Taylor Oct 23 '13 at 16:07
  • @Joshua O.K, let me modify the question. –  Oct 23 '13 at 16:11
  • 1
    sqrt-iter should be undefined if you haven't defined it either. – C. Warren Dale Oct 23 '13 at 16:16
  • @ChrisDale so it's no problem with lang packet. if so, all I need to do is have check on trivia? –  Oct 23 '13 at 16:25
  • sqrt-iter is defined in the text above the exercise. word, is found in the simply.scm file that is linked to in the introduction of simply scheme, you should just load that into your interperter. – WorBlux Oct 23 '13 at 19:08
  • I would add that the code from "Simply Scheme" mentioned, viz https://people.eecs.berkeley.edu/~bh/ssch1/showing.html as found in https://people.eecs.berkeley.edu/~bh/downloads/simply/simply.scm is pretty archaic, and I would not recommend it for a beginner, not that there is anything wrong with it /per se/, but it will not run unmodified on most modern Schemes! Perhaps someone could adapt it to Racket with a #lang module, but unless the object of the excercise is adapting ancient Scheme into modern, it is probably best avoided. – Alex Gian May 29 '18 at 16:48
  • Further to my comment above, I have now upload a Gist of the Racket version of the "Simply Scheme" code already mentioned. You can find it here: https://gist.github.com/alexgian/5b351f367169b40a4ad809f0bb718e1f.js The problem is caused by Racket not allowing the gung-ho redefining and set!ing of functions in the way that so many old Scheme programmers were so fond doing it! ;) As a result, most old Scheme examples need a bit of "cleaning up" to allow them to operate correctly on Racket. – Alex Gian Jun 04 '18 at 13:50
  • Oops, the link I gave above is the Javascript embedding. Here is a better on for the actual Gist: https://gist.github.com/alexgian/5b351f367169b40a4ad809f0bb718e1f – Alex Gian Jun 04 '18 at 14:02
  • If you use scheme it's fine – alinsoar Dec 26 '18 at 18:07

3 Answers3

60

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))))
Seth
  • 2,712
  • 3
  • 25
  • 41
Sylwester
  • 47,942
  • 4
  • 47
  • 79
  • Unfortunately this seems to be somewhat bit rotten. Attempting to install now just gives a bunch of errors. If you have an updated answer to the question that would be great :-) – Steve Homer Jul 07 '14 at 20:07
  • 1
    @SteveHomer Unfortunately newer Racket version are less compatible, but they work even with the errors so I've added info about that. I'll update when the packages are converted to the new package system. – Sylwester Jul 09 '14 at 10:52
  • Can I include other files? `(require neil/sicp/include) (include "2.39.scm")` does't work. – Zen Nov 24 '15 at 07:42
  • @Zen Since it's based on the r5rs module `require` exists under the name `#%require` eg `(#%require srfi/1)` or `(#%require "myfile.scm")`. You may need to add your directory to the collection path. – Sylwester Nov 24 '15 at 21:59
  • 1
    New: in the url you provided they explain that it now can be easily done from within DrRacker itself. In the Package Manager. – Ronen Jan 31 '17 at 08:31
  • I'm using DrRacket 7.7 and I was able to follow (1) to download the sicp language - thank you! – wmock May 22 '20 at 21:24
18

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)
Community
  • 1
  • 1
Yongwei Wu
  • 5,292
  • 37
  • 49
  • Thanks for the more current info! – wmock Jun 08 '16 at 05:20
  • I have 6.5 and putting `#lang sicp` in the file didn't work. Some error would pop up, with a button, but clicking it didn't work. Following the new instructions from Sylwester's answer did work though. – Will Ness May 16 '18 at 06:25
4

[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?
GoZoner
  • 67,920
  • 20
  • 95
  • 145