2

I'm trying to implement a sort of read-write to cell function.

(define (read-write-get cell) (cell (list)))
(define (read-write-set cell x) (cell (list x)))

(define (read-write-cell x)
   (let ((cell '()))
       (read-write-set cell x)))

(define w1 (read-write-cell 10))
(check-equal? 10 (read-write-get w1))

I keep getting the error

application: not a procedure; expected a procedure that can be applied to arguments given: '() arguments...: errortrace...:

Will Ness
  • 70,110
  • 9
  • 98
  • 181
  • 1
    It's very difficult to guess what "a sort of read-write to cell function" would be, but I suspect that you would be helped by SICP's [chapter on assignment and local state](https://mitpress.mit.edu/sites/default/files/sicp/full-text/book/book-Z-H-20.html#%_sec_3.1.1). – molbdnilo Feb 13 '19 at 07:29

1 Answers1

3

In Scheme (x y) means apply the function x to the argument y. So

(define (read-write-set cell x) (cell (list x)))

defines a function read-write-set that, when called with the first parameter which is a function, applies that function, cell, to the result of evaluating (list x) (which build a list with the unique element the second parameter).

Then, in:

(define (read-write-cell x)
   (let ((cell '()))
       (read-write-set cell x)))

You call read-write-set with the first argument which is not a function, but an empty list (since cell is assigned to '() in the let).

So, “not a procedure; expected a procedure” refers to the value of the first argument to read-write-set, which is not a procedure but a list. It is not clear to me the intended behaviour of read-write-get and read-write-set, so I cannot suggest how to correct them.

Renzo
  • 26,848
  • 5
  • 49
  • 61
  • I wrote some tests to describe what I'm attempting to implement (define a1 (read-write-cell 25)) (check-equal? 25 (read-write-get a1)) (define a2 (read-write-set a1 30)) (check-equal? 30 (read-write-get a2)) – Ron Marchant Feb 13 '19 at 16:51
  • @JohnSmith, you should decide how to implement the cell. As a closure (i.e. a function with an environment)? As a modifiable data structure? Or what else? The functions that you want to test are the *interface* to the cell abstraction, but the cell itself must be implemented in some specific, concrete, way. – Renzo Feb 14 '19 at 09:47
  • I would like to implement it using closure. Do you know how I may be able to do that? – Ron Marchant Feb 14 '19 at 21:05
  • @JohnSmith, you can find a detailed discussion in the link provided in the comment of molbdnilo. – Renzo Feb 14 '19 at 21:38