stack
  
 
  stackを見てもコインシリンダーしか思い出せません。コインシリンダー欲しいです。あと再帰的な彼女も欲しいです。
   
    コード
              |           (define make-stack                (lambda ()                  (let ((front '()))                    (lambda (msg . args)                      (cond                       ((eq? msg 'push!)                        (set! front (cons (car args) front))                        (car args))                       ((eq? msg 'pop!)                        (cond                         ((null? front) #f)                         (else (let ((data (car front)))                                 (set! front (cdr front))                                 data))))                       ((eq? msg 'len)                        (length front))                       ((eq? msg 'each)                        (cond                         ((null? args) front)                         (else (map (car args) front))))                       ((eq? msg 'clear)                        (set! front '()))                       ((eq? msg 'empty?)(null? front))                       (else #f))))))         |      
             |           (define s (make-stack))            (s 'empty?)             ;; => #t                       (s 'push! 1)              ;; => 1                       (s 'push! 1)              ;; => 1                       (s 'pop!)              ;; => 1                       (s 'push! 2)              ;; => 2                       (s 'push! 3)              ;; => 3                       (s 'len)              ;; => 3                       (s 'each (lambda (x)(display x)))              ;; => 321(#<undef> #<undef> #<undef>)                       (s 'clear)              ;; => ()                       (s 'pop)              ;; => #f         |      
   
    ほとんどが下記参照先の写経です。len、each、clearを追加しただけ。
   
  参考
    関連
    
 
0 件のコメント:
コメントを投稿