Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>How's this (in Scheme [Dr. Racket]):</p> <pre><code>(define (as-string elm) (cond ((string? elm) (string-append "\\\"" elm "\\\"")) ((number? elm) (number-&gt;string elm)) ((symbol? elm) (symbol-&gt;string elm)) ((null? elm) "*empty-list*") (else (error "Unrecognized type")))) (define (node-name-label names labels) (apply append (map (lambda (a b) (if (list? a) (node-name-label a b) (list (cons a b)))) names labels))) (define (node-txt names labels) (apply string-append (map (lambda (x) (let ((name (car x)) (label (cdr x))) (string-append name " [label=\"" (as-string label) "\"];\n"))) (node-name-label names labels)))) (define (graph-txt lst) (apply string-append (map (lambda (x) (let ((a (car x)) (b (cdr x))) (string-append a " -- " b ";\n"))) (get-relationships lst)))) (define (declare-nodes lst (basename "node")) (map (lambda (x n) (if (and (list? x) (not (empty? x))) (declare-nodes x (string-append basename "_" (number-&gt;string n))) (string-append basename "_" (number-&gt;string n)))) lst (range 0 (length lst)))) (define (get-relationships lst) (if (&lt; (length lst) 2) null (apply append (map (lambda (x) (if (list? x) (cons (cons (car lst) (car x)) (get-relationships x)) (list (cons (car lst) x)))) (cdr lst))))) (define (range start end) (if (&gt;= start end) '() (cons start (range (+ 1 start) end)))) (define (get-graph code graph-title) (let ((names (declare-nodes code))) (string-append "graph " graph-title " {\n" (node-txt names code) "\n" (graph-txt names) "}"))) </code></pre> <p>Usage: <code>(display (get-graph '(+ 2 (f1 (f2 () y) (f2 x y))) "simple_graph"))</code> produces:</p> <pre><code>graph simple_graph { node_0 [label="+"]; node_1 [label="2"]; node_2_0 [label="f1"]; node_2_1_0 [label="f2"]; node_2_1_1 [label="*empty-list*"]; node_2_1_2 [label="y"]; node_2_2_0 [label="f2"]; node_2_2_1 [label="x"]; node_2_2_2 [label="y"]; node_0 -- node_1; node_0 -- node_2_0; node_2_0 -- node_2_1_0; node_2_1_0 -- node_2_1_1; node_2_1_0 -- node_2_1_2; node_2_0 -- node_2_2_0; node_2_2_0 -- node_2_2_1; node_2_2_0 -- node_2_2_2; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload