[Scheme] 循環のあるグラフをコピーする

コンスセルを使った循環のあるグラフをコピーする関数は次のように書ける。

(define (copy-graph-sub ht graph)
  (cond ((not (pair? graph)) graph)
	((hash-table-exists? ht graph)
	 (hash-table-ref ht graph))
	(else
	 (let ((p (cons #f #f)))
	   (hash-table-set! ht graph p)
	   (set-car! p (copy-graph-sub ht (car graph)))
	   (set-cdr! p (copy-graph-sub ht (cdr graph)))
	   p))))

(define (copy-graph graph)
  (copy-graph-sub (make-hash-table) graph))

ハッシュテーブルの操作はSRFI-69に沿っている。
Gauche では hash-table-ref は hash-table-get, hash-table-set! は hash-table-put! と書くみたい。


Deprecated: Creation of dynamic property WP_Term::$cat_ID is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 378

Deprecated: Creation of dynamic property WP_Term::$category_count is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 379

Deprecated: Creation of dynamic property WP_Term::$category_description is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 380

Deprecated: Creation of dynamic property WP_Term::$cat_name is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 381

Deprecated: Creation of dynamic property WP_Term::$category_nicename is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 382

Deprecated: Creation of dynamic property WP_Term::$category_parent is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 383
This entry was posted in コンピュータ and tagged , , by bugyo. Bookmark the permalink.

Deprecated: Creation of dynamic property WP_Query::$comments_by_type is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/comment-template.php on line 1528

Leave a Reply

Your email address will not be published. Required fields are marked *

*