简体   繁体   中英

How to add to embedded list in scheme?

I'm trying to generate a symbol table in scheme and I'm stuck on the set-symbol function. The number corresponds to the block level of the code or "scope".

First symbol it reads in
((c class 0))
Next symbols 
(((c class 0) (a int 0) (b float 0)))
We read a bracket and read the next variables to a new scope.
(((a char 1) (d int 1)) ((c class 0) (a int 0) (b float 0)))
We leave that scope and "pop the stack".
(((c class 0) (a int 0) (b float 0)))

How do I always add to deepest list of the first list in scope?

I suspect you'd be better off using a different representation. One of many, many would be:

(define (make-symbol-table parent alist)
  `(SYMBOL-TABLE ,parent ,@alist))
(define symbol-table-parent cadr)
(define symbol-table-alist  cddr)

(define (symbol-table-depth table)
  (let ((parent (symbol-table-parent table)))
    (if (not parent)
        1
        (+ 1 (symbol-table-depth parent))))

(define (symbol-table-lookup table name)
  (cond ((assoc name (symbol-table-alist table)) => cdr)
        (else (let ((parent (symbol-table-parent table)))
                (and parent (symbol-table-lookup parent name))))))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM