Which lang packet is proper for SICP in Dr.Racket?
Asked Answered
D

3

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

Driscoll answered 23/10, 2013 at 15:35 Comment(16)
Can you link to the part of SICP you're working on?Vasili
Wait a minute, you are trying to run the function word but defined it as word?. You would expect it not to be found...Barnaba
See this chapter in "Simply Scheme":cs.berkeley.edu/~bh/ssch1/showing.html I got the code above.Driscoll
@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.Pulpiteer
@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 .Pulpiteer
@Joshua Taylor But the function word and accumulate should be embedded in #lang scheme, if I didn't get this wrong.Driscoll
@Joshua Thank you, I know it's not exactly the book. But I can't run the code either..Driscoll
@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.)Pulpiteer
@Joshua O.K, let me modify the question.Driscoll
sqrt-iter should be undefined if you haven't defined it either.Vasili
@ChrisDale so it's no problem with lang packet. if so, all I need to do is have check on trivia?Driscoll
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.Shelbyshelden
I would add that the code from "Simply Scheme" mentioned, viz people.eecs.berkeley.edu/~bh/ssch1/showing.html as found in 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.Continue
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: 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.Continue
Oops, the link I gave above is the Javascript embedding. Here is a better on for the actual Gist: gist.github.com/alexgian/5b351f367169b40a4ad809f0bb718e1fContinue
If you use scheme it's fineBackstitch
S
61

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))))
Signify answered 24/10, 2013 at 9:14 Comment(6)
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 :-)Incomprehensible
@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.Signify
Can I include other files? (require neil/sicp/include) (include "2.39.scm") does't work.Exuberant
@Exuberant 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.Signify
New: in the url you provided they explain that it now can be easily done from within DrRacker itself. In the Package Manager.Crocus
I'm using DrRacket 7.7 and I was able to follow (1) to download the sicp language - thank you!Lev
R
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)
Ripieno answered 4/5, 2016 at 6:2 Comment(2)
Thanks for the more current info!Lev
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.Elenore
H
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?
Hyperostosis answered 23/10, 2013 at 17:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.