Name:
Anonymous
2011-01-07 18:59
; With SICP's functional dispatch.
(define make-stack
(λ ()
(let stack- ((stack '()))
(define pop (λ () (if (null? stack) (error 'pop "pop an empty stack") (cons (stack- (cdr stack)) (car stack)))))
(define push (λ (z) (stack- (cons z stack))))
(define dispatch
(λ (z . o)
(case z
((pop) (apply pop o))
((push) (apply push o))
((empty?) (null? stack)))))
dispatch)))
(define x (make-stack))
(((x 'push 3) 'push 4) 'pop)
; (#<procedure:dispatch> . 4)
; With structs
(struct stack (s))
(define make-stack2
(λ ()
(stack '())))
(define stack-push
(λ (s z)
(stack (cons z (stack-s s)))))
(define stack-pop
(λ (s)
(cons (stack (cdr (stack-s s))) (car (stack-s s)))))
(define y (make-stack2))
(stack-pop (stack-push (stack-push y 3) 4))
; (#<stack> . 4)
Name:
Anonymous
2011-01-07 19:18
STACK IS GAY real men use heap
Name:
Anonymous
2011-01-07 22:41
This thread doesn't quite stack up to the others.
Name:
Anonymous
2011-01-08 11:48
Why not just push with cons and pop with car and cdr?