2017年1月26日木曜日

開発環境

計算機プログラムの構造と解釈[第2版](ハロルド エイブルソン (著)、ジュリー サスマン (著)、ジェラルド・ジェイ サスマン (著)、Harold Abelson (原著)、Julie Sussman (原著)、Gerald Jay Sussman (原著)、和田 英一 (翻訳)、翔泳社、原著: Structure and Interpretation of Computer Programs (MIT Electrical Engineering and Computer Science)(SICP))の第3章(標準部品化力、オブジェクト及び状態)、3.3(可変データでのモデル化)、3.3.2(キューの表現)、問題3.23.を取り組んでみる。

その他参考書籍

問題3.23.

コード(Emacs)

(begin
  (define (p obj)
    (display obj)
    (newline))
  
  (define (front-ptr queue) (car queue))
  (define (rear-ptr queue) (cdr queue))
  (define (set-front-ptr! queue item) (set-car! queue item))
  (define (set-rear-ptr! queue item) (set-cdr! queue item))  
  (define (empty-queue? queue) (null? (front-ptr queue)))
  (define (make-queue) (cons '() '()))
  
  (define (front-queue queue)
    (if (empty-queue? queue)
        (error "(front-queue) called with an empty queue" queue)
        (car (front-ptr queue))))
  
  (define (rear-queue queue)
    (if (empty-queue? queue)
        (error "(rear-queue) called with an empty queue" queue)
        (car (rear-ptr queue))))
  
  (define (front-insert-queue! queue item)
    (if (empty-queue? queue)
        (begin
          (define item0 (list item '() '()))
          (set-front-ptr! queue item0)
          (set-rear-ptr! queue item0))
        (begin
          (define item0 (list item '() (front-ptr queue)))
          (set-car! (cdr (front-ptr queue)) item0)
          (set-front-ptr! queue item0)))
    queue)

  (define (rear-insert-queue! queue item)
    (if (empty-queue? queue)
        (begin
          (define item0 (list item '() '()))
          (set-front-ptr! queue item0)
          (set-rear-ptr! queue item0))
        (begin
          (define item0 (list item (rear-ptr queue) '()))
          (set-car! (cddr (rear-ptr queue)) item0)
          (set-rear-ptr! queue item0)))
    queue)
  
  (define (front-delete-queue! queue)
    (if (empty-queue? queue)
        (error "(front-delete-queue!) called with an empty queue" queue)
        (begin
          (set-front-ptr! queue (caddr (front-ptr queue)))
          (set-car! (cdr (front-ptr queue)) '())))
    queue)
  
  (define (rear-delete-queue! queue)
    (if (empty-queue? queue)
        (error "(rear-delete-queue!) called with an empty queue" queue)
        (begin
          (set-rear-ptr! queue (cadr (rear-ptr queue)))
          (set-car! (cddr (rear-ptr queue)) '())))
    queue)

  (define (print-queue queue)
    (define (iter o flag)
      (if (null? o)
          (begin
            (display ")")
            (newline))
          (begin
            (if flag
                (display " "))
            (display (car o))
            (iter (caddr o) #t))))
    (display "(")
    (iter (front-ptr queue) #f))

  (define queue (make-queue))
  (print-queue queue)
  (front-insert-queue! queue 1)
  (print-queue queue)
  (front-insert-queue! queue 2)
  (print-queue queue)
  (rear-insert-queue! queue 3)
  (print-queue queue)  
  (rear-insert-queue! queue 4)
  (print-queue queue)
  (front-insert-queue! queue 5)
  (print-queue queue)  
  (rear-insert-queue! queue 6)
  (print-queue queue)
  (front-delete-queue! queue)
  (print-queue queue)
  (rear-delete-queue! queue)
  (print-queue queue)
  (front-delete-queue! queue)
  (print-queue queue)
  (rear-delete-queue! queue)
  (print-queue queue)
  )

入出力結果(Terminal(ksi), REPL(Read, Eval, Print, Loop))

$ ksi < sample23.scm
ksi> ()
(1)
(2 1)
(2 1 3)
(2 1 3 4)
(5 2 1 3 4)
(5 2 1 3 4 6)
(2 1 3 4 6)
(2 1 3 4)
(1 3 4)
(1 3)
=> #<unspecified>
ksi> $

0 コメント:

コメントを投稿

Comments on Google+: