Methods and properties in the circuit: is OOP possible in the circuit?

I will use a simple example to illustrate my question. In Java, C, or any other OOP language, I could create a class piein the same way:

class Apple{
    public String flavor;
    public int pieces;
    private int tastiness;
    public goodness(){
        return tastiness*pieces;
    }
}

What is the best way to do this with Scheme? I suppose I could do something like this:

(define make-pie
  (lambda (flavor pieces tastiness)
    (list flavor pieces tastiness)))

(define pie-goodness
  (lambda (pie)
    (* (list-ref pie 1) (list-ref pie 2))))

(pie-goodness (make-pie 'cherry 2 5))

;output: 10

... where cherries are aromas, 2 are slices, and 5 is good. However, then there is no security or type visibility, and everything is simply stuffed into the unlabeled list. How can I improve this?

Sidenote: make-pie 3 . , (, , Java C), ( - , ) ?


:

/, . , .

, , , , , pie , , .

+3
3

, , , . OO- - , !

, , , -. ( , .)

(define (make-pie flavor pieces tastiness)
        (lambda (selector)
                (cond ((eqv? selector 'flavor) flavor)
                      ((eqv? selector 'pieces) pieces)
                      ((eqv? selector 'tastiness) tastiness)
                      ((eqv? selector 'goodness) (* pieces tastiness))
                      (else '()))))

pie. flavor, pieces tastiness , , ( ) .

, :

> (define pie1 (make-pie "rhubarb" 8 4))
> (define pie2 (make-pie "pumpkin" 6 7))
> (pie1 'flavor)
"rhubarb"
> (pie1 'goodness)
32
> (pie2 'flavor)
"pumpkin"
+7

, . , .

+3

, , Scheme

(define PersonClass (lambda (name age strength life)
                   (let ((name name)(age age)(life life)(strength strength))
                     (lambda (command data)
                       (cond
                         ((< life 1)
                          "I am dead")
                         ((equal? command "name")
                          name)
                         ((equal? command "age")
                          age)
                         ((equal? command "birthday")
                          (set! age(+ age 1)))
                         ((equal? command "receive damage")
                          (begin (set! life(- life Data)) (display "I received damage\n")))
                         ((equal? command "hit")
                          (data "receive damage" strength))
                         )))))

(Karl "name" 0) ;; return name requires some data to work "0"

+1
source

Source: https://habr.com/ru/post/1748084/


All Articles