graph-utils.scm 812 B

123456789101112131415161718192021222324
  1. (library (graph-utils)
  2. (export routes->path)
  3. (import (except (rnrs base)
  4. let-values
  5. map)
  6. (only (guile)
  7. lambda* λ)
  8. ;; SRFIs
  9. ;; hash tables
  10. (srfi srfi-69))
  11. (define routes->path
  12. (lambda* (routes target #:key (equal-test eq?))
  13. "Constructs the shortest path from the start node to the
  14. target node using a routes table. A routes table is assumed
  15. to be a hash table, which stores the preceding node on a
  16. path to any node."
  17. (let iter ([current-node° target] [path° (list target)])
  18. (let ([prior-node (hash-table-ref routes current-node°)])
  19. (cond
  20. [(equal-test current-node° prior-node) path°]
  21. [else
  22. (iter prior-node (cons prior-node path°))]))))))