Created
June 18, 2019 18:44
-
-
Save mattmight/2f19a35ad53f056b2162898b2d03e5c6 to your computer and use it in GitHub Desktop.
splainer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket | |
(define verbose-mode (make-parameter #f)) | |
(define input-file | |
(command-line | |
#:program "splain" | |
#:once-each | |
[("-v" "--verbose") "Search with verbose messages" | |
(verbose-mode #t)] | |
#:args (effect:causes-file) ; expect one command-line argument: <effect:causes> | |
effect:causes-file)) | |
(define effect:causes (read (open-input-file input-file))) | |
(define (splain effect:causes-list) | |
(define cause=>effects (make-hash)) | |
(define (add-cause! cause effect) | |
(define current-effects (hash-ref cause=>effects cause (set))) | |
(hash-set! cause=>effects cause (set-add current-effects effect))) | |
(for ([e:cs effect:causes-list]) | |
(define e (car e:cs)) | |
(define cs (cadr e:cs)) | |
(for ([c cs]) | |
(add-cause! c e))) | |
(define (c:es< e1 e2) | |
(< (set-count (cdr e1)) (set-count (cdr e2)))) | |
(define cause=>effects* (hash->list cause=>effects)) | |
(define sorted-cause=>effects (sort cause=>effects* c:es<)) | |
(map (lambda (e) | |
(match e | |
[`(,cause . ,effects) `(,cause ,(set->list effects))])) | |
sorted-cause=>effects)) | |
(pretty-write (splain effect:causes)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment