创建一个像对象一样的过程 - Scheme

时间:2017-11-13 19:04:36

标签: scheme racket

我正在尝试创建一个充当对象类的过程。比如拥有自己的属性和方法。我正在尝试创建一个绑定到变量的make-list过程(定义L1(make-list)),但是我遇到了实现属性local-list的问题,local-list是有界的存储列表。变量L1。我的问题是无论我如何改变(定义local-list'(1 2 3 4)) - 我的打印列表程序总是空的

  (define (make-list)
    ;;Helper procedures
    (define (print-list list)
      (if (eq? list '())
        (display "Empty")
        (begin (car list)
          (print-list (cdr list)))))
    (begin
      (define local-list '(1 2 3 4))
      (lambda (x)
        (cond 
          ((eq? x 'size)
           (begin 
             (display "L1: ")
             (newline)
             (local-list)
             (print-list (local-list))))
           (else #f)))

1 个答案:

答案 0 :(得分:0)

你非常接近

(define (make-list)

  ;;Helper procedures
  (define (print-list list)
    (if (eq? list '())
        (display "Empty")
        (begin (displayln (car list)) ; <-display missing
               (print-list (cdr list)))))
  ; no begin here
  (define local-list '(1 2 3 4))

  (lambda (x)
    (cond 
      ((eq? x 'size)
       (begin 
         (display "L1: ")
         (newline)
         (print-list local-list))) ; <- no call to local-list (2x)
      (else #f))))

测试

> (define L1 (make-list))
> (L1 'size)
L1: 
1
2
3
4
Empty
>