Guest User

Untitled

a guest
Jun 23rd, 2026
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 1.33 KB | None | 0 0
  1. (define-module (hello-lor)
  2.   #:use-module (ice-9 regex)
  3.   #:use-module (srfi srfi-1))
  4.  
  5. (define data
  6.   "a → b, c, d, z
  7. b → d, e, f, z
  8. c → d, e, f, z
  9. d → e, f, z
  10. e → f, z
  11. f → g, h, j, z
  12. g → h, i, j, z
  13. h → i, j, k, z
  14. i → j, k, l, z
  15. j → k, m, z
  16. k → l, n, o, z
  17. l → m, z
  18. m → n, o, z
  19. n → o, z
  20. o → p, z
  21. p → q, z
  22. q → r, z
  23. r → s, z
  24. s → t, u, z
  25. t → u, v, z
  26. u → v, w, z
  27. v → w, z
  28. w → x, y, z
  29. x → y, z
  30. y → z")
  31.  
  32. ;; graph[to] <- [from1, from2,fromN]
  33. (define graph (make-hash-table 26))
  34. ;; counter[to] <- num of paths from a to to
  35. (define counter (make-hash-table 26))
  36. (hash-set! counter "a" 1)
  37.  
  38. (define (add-from-path from to)
  39.   (let ((froms (hash-ref graph to #nil)))
  40.     (hash-set! graph to (cons from froms))))
  41.  
  42. (define (register-path path)
  43.   (let ((from (car path))
  44.     (tos (cdr path)))
  45.     (map
  46.      (λ (to)
  47.        (add-from-path from to))
  48.      tos)))
  49.  
  50. (define (build-graph data)
  51.   (let ((from->to-list (map
  52.              (λ (from->tos)
  53.                (map match:substring (list-matches "(\\w)" from->tos)))
  54.              (string-split data #\Newline))))
  55.     (map register-path from->to-list)))
  56.  
  57. (define (count-paths to)
  58.   (or
  59.    (hash-ref counter to)
  60.    (hash-set! counter to
  61.           (fold
  62.            (λ (to acc)
  63.          (+ acc (count-paths to)))
  64.            0
  65.            (hash-ref graph to)))))
  66.  
Advertisement
Add Comment
Please, Sign In to add comment