Search code examples
schememessageprocedure

MIT Scheme Passing Messages Grade Table


At the moment, as part of an assignment, I am attempting to write a scheme program that accepts 3 messages, 'get-student-average 'get-assignment-average and 'add-grade, and returns the information as necessary. 'add-grade basically gives the student id, assignment, and grade as it is inserted in that order. I am still working on the student and assignment average messages (hence the semicolons in front of them), but what keeps failing when I try to run it is the 'add-grade message. It keeps saying that grades is an unbound variable. This is the code that I am testing it with:

(define (make-grades)
  (define T '())
  (define (dispatch op)
    ((eq? op 'add-grade) (lambda(id as gr) (set! T (append T (list (list id as gr)
    )))))
   ;((eq? op 'get-student-average) (lambda(x) ( )))
   ;((eq? op 'get-assignment-average)
))

(define grades (make-grades))
((grades 'add-grade) 7 1 85))

I'm not sure what it is that I am doing wrong to try and get back the information with that. I thought that, as with tables in scheme, the trick is in appending the list as a list to a null value, then setting it.

There are a bunch of other test cases (including some for the 'average messages), but I did not include them as I feel that it's just repeating more of the same. The one other thing I feel I should mention is that all of the test-cases are held together within a list, starting with '( and ending with ).


Solution

  • There are a couple of problems with your code. For starters, you are not actually checking what message is being received (there should be a cond or a series of nested ifs or a case somewhere). And you're not returning the dispatcher procedure. Here, try this to get started:

    (define (make-grades)
      (define T '())
      (define (dispatch op)
        (cond ((eq? op 'add-grade)
               (lambda (id as gr)
                 (set! T (append T (list (list id as gr))))))
              ;((eq? op 'get-student-average) (lambda(x) ( )))
              ;((eq? op 'get-assignment-average)
              ))
      dispatch)
    

    Also, you should add a fourth message to return the list, for testing purposes.