方案-内存系统

发布于 2024-08-18 03:27:06 字数 742 浏览 4 评论 0原文

我正在尝试制作一个存储系统,您可以在内存槽中输入一些内容。所以我正在做的是制作一个 Alist,成对的 car 是内存位置,cdr 是 val。我需要程序来理解两条消息:读和写。读取仅显示所选的内存位置以及分配给该位置的 val,写入会更改该位置或地址的 val。如何制作我的代码,以便它读取您想要的位置并写入您想要的位置?请自行测试一下。任何帮助将不胜感激。这就是我所拥有的:

(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))

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))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

郁金香雨 2024-08-25 03:27:06

一个可能的解决方案:

(define (make-memory n)
  (let ([memory  (make-vector n 0)]
        [protect (lambda (addr) 
                   (if (and (addr . >= . 0) (addr . < . n)) 
                     addr 
                     (error "access to outside of memory")))])
    (match-lambda*
     [`(read  ,addr)    (cons addr (vector-ref memory (protect addr)))]
     [`(write ,addr ,x) (vector-set! memory (protect addr) x)])))

这具有不使用列表(为了速度)并防止恶意尝试访问预分配范围之外的内容的额外好处;)。
按预期工作:

> (define mem (make-memory 10))
> (mem 'read 0)
(0 . 0)
> (mem 'read 2)
(2 . 0)
> (mem 'write 2 10)
> (mem 'read 2)
(2 . 10)
> (mem 'read 100)
access to outside of memory

如果您刚刚开始使用Scheme,这可能有点难以理解。
您可以在此处阅读有关 match-lambda 及其朋友的更多信息。
向量相当于其他语言中的数组(
阅读本文)。

A possible solution:

(define (make-memory n)
  (let ([memory  (make-vector n 0)]
        [protect (lambda (addr) 
                   (if (and (addr . >= . 0) (addr . < . n)) 
                     addr 
                     (error "access to outside of memory")))])
    (match-lambda*
     [`(read  ,addr)    (cons addr (vector-ref memory (protect addr)))]
     [`(write ,addr ,x) (vector-set! memory (protect addr) x)])))

This has the added benefit of not using alists (for speed) and protecting against malicious attempts to access stuff outside the preallocated range ;).
Works as desired:

> (define mem (make-memory 10))
> (mem 'read 0)
(0 . 0)
> (mem 'read 2)
(2 . 0)
> (mem 'write 2 10)
> (mem 'read 2)
(2 . 10)
> (mem 'read 100)
access to outside of memory

This might be a bit hard to grasp if you're just starting out with Scheme.
You can read more about match-lambda and friends here.
Vectors are Scheme's equivalent of arrays in other languages (read this).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文