Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (define-module (hello-lor)
- #:use-module (ice-9 regex)
- #:use-module (srfi srfi-1))
- (define data
- "a → b, c, d, z
- b → d, e, f, z
- c → d, e, f, z
- d → e, f, z
- e → f, z
- f → g, h, j, z
- g → h, i, j, z
- h → i, j, k, z
- i → j, k, l, z
- j → k, m, z
- k → l, n, o, z
- l → m, z
- m → n, o, z
- n → o, z
- o → p, z
- p → q, z
- q → r, z
- r → s, z
- s → t, u, z
- t → u, v, z
- u → v, w, z
- v → w, z
- w → x, y, z
- x → y, z
- y → z")
- ;; graph[to] <- [from1, from2,fromN]
- (define graph (make-hash-table 26))
- ;; counter[to] <- num of paths from a to to
- (define counter (make-hash-table 26))
- (hash-set! counter "a" 1)
- (define (add-from-path from to)
- (let ((froms (hash-ref graph to #nil)))
- (hash-set! graph to (cons from froms))))
- (define (register-path path)
- (let ((from (car path))
- (tos (cdr path)))
- (map
- (λ (to)
- (add-from-path from to))
- tos)))
- (define (build-graph data)
- (let ((from->to-list (map
- (λ (from->tos)
- (map match:substring (list-matches "(\\w)" from->tos)))
- (string-split data #\Newline))))
- (map register-path from->to-list)))
- (define (count-paths to)
- (or
- (hash-ref counter to)
- (hash-set! counter to
- (fold
- (λ (to acc)
- (+ acc (count-paths to)))
- 0
- (hash-ref graph to)))))
Advertisement
Add Comment
Please, Sign In to add comment