PasteRack.org
Paste # 83102
2017-08-13 17:39:30

Fork as a new paste.

Paste viewed 92 times.


Embed:

#lang racket

Problem 2. The symbolic differentiator uses Scheme numbers to represent numbers, Scheme symbols to represent variables, and Scheme lists to represent expressions. For example:
3  is the Scheme number "3" and represents the numeric value of 3
'x  is a Scheme symbol and represents the variable x
'(* 3 x)  is a list and is used to represent the expression 3x
Suppose instead that we use object-oriented approaches in our underlying representation of numbers, variables, and expressions.
Note: To run this code, start with a blank Racket buffer and begin by copy-pasting in these new abstractions. Do not carry forward the implementations from the prior assignment problem and the text.
For example, we will implement numbers like this:
; constructor
(define (make-number n)
  (lambda (msg)
    (cond ((eq? msg 'number?) #t)
          ((eq? msg 'value) n)
          ((eq? msg 'display) n)
          (else #f))))
; predicate
(define (number? expr)
  (expr 'number?))
; selector
(define (value expr)
  (expr 'value))
Here is an implementation for variables:
; constructor
(define (make-variable v)
  (lambda (msg)
    (cond ((eq? msg 'variable?) #t)
          ((eq? msg 'value) v)
          ((eq? msg 'display) v)
          (else #f))))
; predicate
(define (variable? expr)
  (expr 'variable?))
And here is an implementation for a binary expression, which could be a sum or product expression depending on the tag:
(define (make-binary tag e1 e2)
  (lambda (msg)
    (cond ((eq? msg 'binary?) #t)
          ((eq? msg 'tag) tag)
          ((eq? msg 'first) e1)
          ((eq? msg 'second) e2)
          (else #f))))
(define (make-sum e1 e2)
  (make-binary '+ e1 e2))
Let's also provide a display procedure, which simply passes the 'display message to a number, variable, or binary expression object:
(define (display expr)
  (expr 'display))
At this point, you can do the following things:
> (define three (make-number 3))
> three
#<procedure>
> (display three)
3
> (define x (make-variable 'x))
> (display x)
'x
> (define my-expr (make-sum three x))
> (display my-expr)
#f    ; whoops, this doesn't work yet!
Problem 2a. Extend the implementation of the binary-expression object so that it recursively displays its tag and first and second argument when given the 'display message.
When successful, you should see:
> (define my-expr (make-sum three x))
> (display my-expr)
'(+ 3 x)

=>

prog:50:0: read: bad syntax `#<'
  context...:
   /home/pasterack/racket68/collects/syntax/module-reader.rkt:190:25: loop
   /home/pasterack/racket68/collects/syntax/module-reader.rkt:183:2: wrap-internal
   /home/pasterack/racket68/collects/racket/../syntax/module-reader.rkt:65:9: lang:read-syntax
   /home/pasterack/racket68/share/pkgs/scribble-lib/scribble/private/manual-code.rkt:112:0: get-tokens
   /home/pasterack/racket68/share/pkgs/scribble-lib/scribble/private/manual-code.rkt:56:0: typeset-code15
   /home/pasterack/pasterack/tmp/83102/83102code.scrbl: [running body]
   loop
   ...cket/cmdline.rkt:179:51
   /home/pasterack/racket68/share/pkgs/scribble-lib/scribble/run.rkt: [running body]