Trying to build a very simple stack structure :
(define (make-stack lst)
(lambda message
(case (car message)
((see) (newline) (write lst))
((empty?) (null? lst))
((push) (begin
(set-cdr! lst (list (car lst) (cdr lst)))
(set-car! lst (cadr message))))
((pop) (let ((elt (car lst)))
(set-car! lst (cadr lst))
(set-cdr! lst (cddr lst))
elt)))))
I can use it with:
(define L (make-stack (list 0)))
(L 'push 1)
(L 'push 2)
(L 'see)
(newline)
(write (L' pop))
(L 'see)
But I can't init it using an empty list, that's why I init my instantiation using (list 0). How to modify make-stack procedure for it to accept initialization with an empty list?
Edit: When I pass an empty list:
;The object (), passed as the first argument to `cdr`, is not the correct type.
(make-pile (list))?(list 1 (list 2 3))to(cons 1 (list 2 3)). And think about what you get when you add the head of a list to the tail of the same list.