Adding "Simply Scheme" language to DrRacket
Asked Answered
D

3

5

I want to work through this book: http://www.eecs.berkeley.edu/~bh/ss-toc2.html. But I'm having trouble to get the "Simply Scheme" language working. The code won't run.

    #lang planet dyoo/simply-scheme:2
    (parse ’(4 + 3 * 7 - 5 / (3 + 4) + 6))

I keep getting the following error message: "parse: unbound identifier in module in: parse".

Desiree answered 5/10, 2013 at 22:43 Comment(1)
Just to reiterate: you haven't defined parse, so the error message is correct: it hasn't been "bound" because there's no built-in definition for parse. So you were using the language properly: the language wasn't the source of the issue.Proximity
G
6

Take a look at this page, it has complete instructions. Simply do this:

#lang racket
(require (planet dyoo/simply-scheme:2:2))

Also be aware that the character is incorrect, for quoting use ', this probably happened because you copy-pasted code with wrong typesetting.

And of course, after the above is done, you have to define the procedures explained in chapter 18, they're not defined in the package you just imported! this will work for sure:

(define (parse expr)
  (parse-helper expr '() '()))

(define (parse-helper expr operators operands)
  (cond ((null? expr)
     (if (null? operators)
         (car operands)
         (handle-op '() operators operands)))
    ((number? (car expr))
     (parse-helper (cdr expr)
               operators
               (cons (make-node (car expr) '()) operands)))
    ((list? (car expr))
     (parse-helper (cdr expr)
               operators
               (cons (parse (car expr)) operands)))
    (else (if (or (null? operators)
              (> (precedence (car expr))
             (precedence (car operators))))
          (parse-helper (cdr expr)
                (cons (car expr) operators)
                operands)
          (handle-op expr operators operands)))))

(define (handle-op expr operators operands)
  (parse-helper expr
        (cdr operators)
        (cons (make-node (car operators)
                 (list (cadr operands) (car operands)))
              (cddr operands))))

(define (precedence oper)
  (if (member? oper '(+ -)) 1 2))

(define (compute tree)
  (if (number? (datum tree))
      (datum tree)
      ((function-named-by (datum tree))
         (compute (car (children tree)))
         (compute (cadr (children tree))))))

(define (function-named-by oper)
  (cond ((equal? oper '+) +)
    ((equal? oper '-) -)
    ((equal? oper '*) *)
    ((equal? oper '/) /)
    (else (error "no such operator as" oper))))

(parse '(4 + 3 * 7 - 5 / (3 + 4) + 6))
=> '(+ (- (+ (4) (* (3) (7))) (/ (5) (+ (3) (4)))) (6))

(compute (parse '(4 + 3 * 7 - 5 / (3 + 4) + 6)))
=> 30 2/7
Groundless answered 5/10, 2013 at 23:50 Comment(2)
@Desiree I found a second error in the code in the question, you're using instead of ' for quoting. Other than that, you simply have to copy the definitions in chapter 18, as shown above in my updated answer.Insulate
I didn't notice that the quoting is wrong. It's working now! :) Thank you.Desiree
H
2

There is also a module allowing you to run "Simply Scheme" here: https://gist.github.com/alexgian/5b351f367169b40a4ad809f0bb718e1f

You can just run it and then type your code in interactively, no need for an installation like the Planet/dyoo code mentioned above. Should you wish to, though, you can also install it, so that all you have to do then is to prefix any program with #lang simply-scheme and it will work. Instructions for how to do so can be seen here: https://groups.google.com/forum/#!topic/racket-users/jHPtw3Gzqk4 in the message by Matthew Butterick, June 4 2018.

This version takes care of some stuff not done the dyoo code, for instance

> (first 'american)
'a
> (first 'American)
"A"
> (every butfirst '(AmericAn Legacy CODE))
'("mericAn" egacy "ODE")

I.e. incorrect mixing of strings and symbols, instead of

> (first 'american)
a
> (first 'American)
A
> (every butfirst '(AmericAn Legacy CODE))
(mericAn egacy ODE)    

as it should be. The dyoo implementation might be more complete for docs, etc, though

However, as noted in the answer above, you will still have to enter the code for the parse function yourself, as this was the intention of the authors.

Hautbois answered 6/6, 2018 at 10:41 Comment(1)
PS. The abovementioned module works fine with Racket 6.3, no need to revert!Hautbois
C
0

I had to revert to DrRacket 5.4.1 in order to get both Simply Scheme and SICP to work.

Coaxial answered 31/3, 2014 at 5:30 Comment(1)
I see you answered this in March 2014, so things may have changed by I am running DrRacket 6.12 without any problems for the above-mentioned code. (Just FYI, in case anyone thinks that there are backward compatibility problems. It's all good ATM.)Hautbois

© 2022 - 2024 — McMap. All rights reserved.