Scheme - Memory System

Posted by Eric on Stack Overflow See other posts from Stack Overflow or by Eric
Published on 2010-01-15T05:00:36Z Indexed on 2010/06/08 20:52 UTC
Read the original article Hit count: 380

Filed under:
|
|

I am trying to make a memory system where you input something in a slot of memory. So what I am doing is making an Alist and the car of the pairs is the memory location and the cdr is the val. I need the program to understand two messages, Read and Write. Read just displaying the memory location selected and the val that is assigned to that location and write changes the val of the location or address. How do I make my code so it reads the location you want it to and write to the location you want it to? Feel free to test this yourself. Any help would be much appreciated. This is what I have:

(define make-memory
  (lambda (n)
    (letrec ((mem '())
             (dump (display mem)))
      (lambda ()
        (if (= n 0)
            (cons (cons n 0) mem) mem)
            (cons (cons (- n 1) 0) mem))
      (lambda (msg loc val)
        (cond
          ((equal? msg 'read) (display 
                               (cons n val))(set! n (- n 1)))
          ((equal? msg 'write) (set! mem 
                                     (cons val loc)) (set! n (- n 1)) (display mem)))))))


(define mymem (make-memory 100))

© Stack Overflow or respective owner

Related posts about Scheme

Related posts about plt-scheme