How to use GraphViz and Racket - racket

Is it possible to use the graphviz module to draw graphs in a racket frame (GUI)?
If it's possible, would anyone a tutorial that shows how to use?
Thank's

Here is what I would do:
generate a dot-file (the Graphviz format) from the graph
use system to run grapviz on the file producing a png file
display the png in a racket frame
For actual code see: https://github.com/wangkuiyi/graphviz-server
Note that Stephen Chang's graph library has support for generating dot-files: http://pkg-build.racket-lang.org/doc/graph/index.html#%28part._.Graphviz%29
Update:
In order to make a graph editor you can save the graph data in a file, then let Graphviz output layout information in the dot-format: http://www.graphviz.org/doc/info/output.html#d:xdot1.4
Parse the output file and then redraw the graphs on screen.

Here is an example. You must have the dot program installed.
#lang racket
(require graph)
(require racket/gui/base)
(define dot-command "/usr/local/bin/dot -Tpng ~a >~a")
;; Given a graph, use dot to create a png and return the path to the png
(define (make-graphviz-png g)
(let ([dot-string (graphviz g #:colors (coloring/brelaz g))]
[dot-file (make-temporary-file "example~a.dot")]
[png-file (make-temporary-file "example~a.png")])
(display-to-file dot-string dot-file #:exists 'replace)
(system (format dot-command dot-file png-file))
png-file))
;; The graph
(define test-graph (unweighted-graph/directed '((hello world))))
;; Generate the png from the graph
(define bitmap (read-bitmap (make-graphviz-png test-graph)))
;; Display in a frame -- see https://stackoverflow.com/questions/5355251/bitmap-in-dr-racket
(define f (new frame% [label "Your Graph"]))
(new message% [parent f] [label bitmap])
(send f show #t)

You can use the graphviz package. If you want to use the dot language directly, you can use the dot->pict function. Alternatively, you can use digraph->pict, which makes dot a bit more powerful by allowing using Racket picts as node shapes.

Related

let is not defined in DrRacket

I'm trying to learn Racket by using DrRacket and when run this code:
(define f2!
(let ([n 0])
(lambda()
(set​! ​n (add1 n))
n)))
I get an exception: let: this function is not defined.
What am I doing wrong?
Thank you
I've found a solution, I had to pick racket as the main language and also add:
#lang racket
at the top of the file.
Just so you know, the #lang at the top of your file determines what set of actions you are permitted to and also the computation algorithm (Applicative/normal evaluator).
I basically add #lang racket as a basic to all files.

How to add more items to a list in Elisp

I don't know any ELisp, but I need to configure the list of languages to use evaluate source code blocks inline using Org-Babel.
Using this site, I was able to get Python source-code blocks executed. However, I would now like to add support for other languages like C, C++, Matlab and others.
I know I must add identifiers as mentioned on this site to the variable org-babel-load-languages. How do I add the extra elements to
this list?
Currently I am setting the variable org-babel-load-languages via,
this code in my .spacemacs file
(org-babel-do-load-languages
'org-babel-load-languages
'((python . t)))
The enabled languages is a list (...) where each list item looks something like (python . t)
So you could extend your current code like so:
(org-babel-do-load-languages
'org-babel-load-languages
'((python . t)
(c . t)
(cpp . t)
(matlab . t)))
I'm using something like this to generate the same result:
(eval-after-load "org"
'(org-babel-do-load-languages
'org-babel-load-languages
(mapcar (lambda (lang) (cons lang t))
'(python c cpp matlab))))

Redirect input in Clojure

I want to redirect the standard input in my Clojure benchmark. I have passed the file as an argument and I am trying to find the equivalant code in java:
System.setIn(new FileInputStream(filename));
but for Clojure.
The main issue is that I use the DaCapo suite to calculate the performance of Benchmark and the method that loads the benchmark does not recognize special characters like "<" in contrast to the cmd (running the benchmark's jar directly from the cmd..).
This is what I am trying to do..but still does not work.. I think that br has the standard input from the in and it is used by the rest of the program. How can I change the in while I have in the args the desired path, so I can run the benchmark correctly? This is my effort with the "system/setin"
(defn -main [& args]
(let [max-dna-chars-per-line 60
jk (with-open [is (clojure.java.io/input-stream (first args))]
(System/setIn is))
br (java.io.BufferedReader. *in*)
bw (java.io.BufferedWriter. *out* (* 16 8192)) ; 16 * default size, I think
;; We could use the map complement-dna-char-map instead of
;; complement-dna-char-fn, but when I tested that, the program
;; spent a lot of time running the hashCode method on
;; characters. I'm hoping this is faster.
complement-dna-char-vec (make-vec-char-mapper complement-dna-char-map)]
(loop [[desc-str dna-seq-str more] (fasta-slurp-br br)]
(println-string-to-buffered-writer bw desc-str)
(print-reverse-complement-of-str-in-lines bw dna-seq-str
complement-dna-char-vec
max-dna-chars-per-line)
(when more
(recur (fasta-slurp-br br))))
(. bw flush)))
System.in is normally wrapped in *in*. It is a dynamic binding intended to be rebound in local scope with binding, like so:
(with-open [is (clojure.java.io/reader "/tmp/foo.txt")]
(binding [*in* is]
(println (read-line))))
Note that it is a wrapper of System.in, not System.in itself, and changing this binding does not change System.in. It only affects the Clojure functions that use *in*, such as read-line here.
If you are calling some Java code that wants to read System.in, you have to call System.setIn:
(with-open [is (clojure.java.io/input-stream filename)]
(System/setIn is)
(do-stuff))
These are the options in Clojure and Java, but I still am not sure I understand your use case. Please clarify in comments if that does not quite make sense.
EDIT: Regarding your program, if you are creating the buffers, do you really need to create them off *in*? What's the difference vs. just opening your own stream?
After trying so many different things I finally found the answer...
I had to use the file reader inside the buffer so the code is like this:
(defn -main [& args]
(let [max-dna-chars-per-line 60
br (java.io.BufferedReader. (clojure.java.io/reader (first args)))
bw (java.io.BufferedWriter. *out* (* 16 8192)) ; 16 * default size, I think
where the args contain the filename ...

Best way to define an Image variable in racket

Both, the official racket tutorial and the book, "Realm of Racket" suggest using Dr. Racket to load and define images in Racket.
I however am using Geiser (racket-repl) with Emacs. I define images using the
(make-object bitmap% (image-location))
function.
Is this the best way to load an image? Or are there more efficient and easy ways?
I ask because I was confronted with this problem.
If you are already using the 2htdp/image library (which your other SO post suggests), then you may wish to use the bitmap function from the same library. See this section of the documentation for 2htdp/image.
You can use it like this:
#lang racket
(require 2htdp/image)
(define my-image (bitmap "path/to/image.png"))

How do I include files in DrScheme?

I'm using DrScheme to work through SICP, and I've noticed that certain procedures (for example, square) get used over and over. I'd like to put these in a separate file so that I can include them in other programs without having to rewrite them every time, but I can't seem to figure out how to do this.
I've tried:
(load filename)
(load (filename))
(load ~/path-to-directory/filename)
(require filename)
(require ~/path-to-directory/filename)
(require path-from-root/filename)
None of these works. Obviously I'm grasping at straws -- any help is much appreciated.
It's not clear from your question what language level you're using; certain legacy languages may make certain mechanisms unavailable.
The best inclusion/abstraction mechanism is that of modules.
First, set your language level to "module". Then, if I have these two files in the same directory:
File uses-square.ss:
#lang scheme
(require "square.ss")
(define (super-duper x) (square (square x)))
File square.ss :
#lang scheme
(provide square)
(define (square x) (* x x))
Then I can hit "run" on the "uses-square.ss" buffer and everything will work the way you'd expect.
Caveat: untested code.
I believe you are looking for:
(include "relative/path/to/scheme/file.scm")
The (require) expression is for loading modules.
In MIT/GNU Scheme, you can load a file with something like this:
(load "c:\\sample-directory\\sample-file.scm")
But I do not know if it works in DrScheme.
(require "~/path-to-directory/filename")