4
votes

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".

3
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. - dyoo

3 Answers

5
votes

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
1
votes

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.

0
votes

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