Has someone implemented the OO-syntax for Harvey's cs61a?

Hi,

I'm following Harvey's course and I need to run the OO examples using the library they developed (here).

I am not able to load it since #lang simply-scheme does not define define-macro.

Does anyone by chance implement this OO model?

PS: I am not able to use UCB's Scheme since it does not run on M1 chips.

Best Regards,

Daniel

1 Like

Try

sicp/obj.rkt at main · zackads/sicp · GitHub

Hi @soegaard, thanks for the suggestion.

I've tried to use it for two examples and got two different errors.

In the first example, I got an error that I can circumvent by declaring a dummy instance-var. This is the error:

obj.rkt:156:5: result arity mismatch;
expected number of values not received
expected: 2
received: 1
in: local-binding form
arguments...:
  '()

This is the original code

#lang racket
(require "obj.rkt" rackunit)

(define-class (account balance)
   ;(instance-vars (dummy-var 0))    ; -> Commented to generate the error
  (method (deposit amount)
      (set! balance (+ amount balance))
      balance)
  (method (withdraw amount)
      (if (< balance amount)
          "Insufficient funds"
          (begin
            (set! balance (- balance amount))
            balance))))

The second error I got when building a class inheriting from this. Here's the error:

my-account: unbound identifier in: my-account

And here's the code that generated the error:

(define-class (checking-account init-balance)
   (parent (account init-balance))
   (instance-vars (check-fee 0.10))
  (method (write-check amount)
      (ask self 'withdraw (+ amount check-fee)))
  (method (set-fee! fee)
      (set! check-fee fee)))

This second error I can't circumvent.

Thank you

Default-method is generating an error as well:

(define-class (squarer)
    (instance-vars (dummy 0)) 
    (default-method (* message message))  
    (method (7) 'buzz) )

The error it generates:

message: unbound identifier
context...:
other binding...:
other binding...:
common scopes...: in: message

I think, you will get a better experience using the builing objects.

#lang racket

(define account
  (class object% ; parent
    (super-new)

    (init-field balance)

    (define/public (deposit amount)
      (set! balance (+ amount balance))
      balance)

    (define/public (withdraw amount)
      (if (< balance amount)
          "Insufficient funds"
          (begin
            (set! balance (- balance amount))
            balance)))))

(define my-account (new account [balance 20]))
(send my-account deposit 100)
(send my-account withdraw 20)


(define checking-account
  (class account ; parent
    (init init-balance)
    (super-new [balance init-balance])
    (define check-fee 0.10)
    
    (define/public (write-check amount)
      (send this withdraw (+ amount check-fee)))
    
    (define/public (set-fee! fee)
      (set! check-fee fee))))

(define my-account2 (new checking-account [init-balance 20]))
(send my-account2 write-check 10)