Is there an emacs-lisp command which I could use to plot a collection of numbers with gnuplot, much like I can create simple line charts in Excel from an array of numbers?
For example, I would like to have a function called something like plot-with-gnuplot which I could call with something like:
(plot-with-gnuplot '((0 0.1) (0.1 1) (0.5 10)))
and have this produce a simple line plot going through those points.
Ultimately I will be using this to view mathematical functions that I am writing, it would be useful to be able to have a quick look at how my code is coming along by drawing a simple line plot of its values over a certain range.
It is not exactly what you want but you may find Org-babel-gnuplot from org-mode useful. I've made an example file, it should work if you have gnuplot.el and emacs24. Run the first code block (C-c C-c), and then run the third code block.
test.org:
#+BEGIN_SRC emacs-lisp :results silent
;; load gnuplot mode
(require 'gnuplot "~/Dropbox/emacs/gnuplot.el")
(require 'ob-gnuplot)
#+END_SRC
#+NAME: edata
#+BEGIN_SRC emacs-lisp
'((0 0.1) (0.1 1) (0.5 10)))
#+END_SRC
#+BEGIN_SRC gnuplot :file file.png :var data=edata
plot data w lp
#+END_SRC
Related
I was wondering if you're allowed to call another program you made to do work in a new program you are about to make in scheme, or would I have to copy and paste all the code from the previous program, though I can do this and make my life simple, I'm wondering if there is a more sophisticated elegant way to import in scheme r5rs.
;; Here is an example of what I mean
;; for example I have a file name add.scm which adds 2 numbers
;; now I want to make a new function that squares the numbers
;;is there a way I can do something like this
#lang add.scm as add
;;and use it here like so
(define (square x)
(* x x))
(square add)
;;where add already returns a value
This may seem like a silly piece of code, but this is the best pesudo code I can give, if the example needs more editing to understand please let me know, thank you.
If I understand your question, you have this module named add.scm:
;; add.scm
#lang racket
(+ 5 7) ;; prints 12 when run
and you want to write a new module that gets the value that the add.scm module prints out, bind it to a variable, and use it in a computation, like this:
;; square.scm
#lang racket
(??? add ??? "add.scm" ???) ;; bind `add` to "value" of "add.scm" module
(* add add) ;; prints 144 when run
No, there is no easy way to do that. It's possible, by using dynamic-require or a subprocess to run the module, capturing the output and then reading it to get the value, but that's an awful, complicated, brittle way to do things.
In Racket, a better approach would be for add.rkt to define and export a variable and have a main submodule that prints the value:
;; add.rkt
#lang racket
(provide x)
(define x (+ 5 7))
(module+ main
x)
Then you can use (require "add.rkt") from another module to get x to use in computations, and if you run add.rkt as a program (for example, with racket add.rkt), then it prints the value.
Racket (which has #lang as in the question, and module/provide/require as explained in another answer) is not Scheme (let alone R5RS).
In R6RS Scheme, Scheme source can be divided between library files and top-level programs
If the file add-library.scm contains:
(library (add-library)
(export add)
(import (rnrs))
(define add (+ 5 7)) )
and add-toplevel.scm contains:
(import (add-library))
(define (square x)
(* x x))
(display (square add))
Then:
$ scheme
> (load "add-toplevel.scm")
144
>
I would like to redefine a standard Racket function, namely display, within a lexical scope, as in this example:
(with-custom-display (display "hello"))
This should work even when the code within the scope of with-custom-display is from a different library or even racket/* packages.
Is this possible? If so, how to do it?
EDIT:
If not possible in general, at least for the case of display and other write functions... could I somehow transform every output by parameterizing on current-output-port then redirecting the transformed output to the original port?
While its not possible1 to globally replace arbitrary functions in Racket, you absolutely CAN change the standard out port that a Racket program uses (and by extension, functions like display). In fact, this is exactly what the readline collection in racket does, except for input ports rather than output ports.
Basically, all you need to do is parameterize current-output-port globally to be your special port. Since you want to ultimately write out to the original output port (but with colors), you can also grab the original output port before changing it to the new one. Your resulting code would look something like this:
#lang racket/base ;; init.rkt
(define orig-port (current-output-port))
(define new-output-port
.... uses orig-port ....)
(current-output-port new-ouput-port)
(replacing .... uses orig-port .... with the implementation of your new colored output port)
And now, any file that requires "init.rkt" will get color in its default output port.
(Note though that if you have multiple files that do this same sort of thing, you have to be careful to make sure that they don't happen in an unsafe order.)
You can also make your with-custom-display form as a simple language extension by doing:
#lang racket ;; custom-disp.rkt
(provide with-custom-display)
(require syntax/parse/define)
(define orig-port (current-output-port))
(define new-output-port
.... uses orig-port ....)
(define-simple-macro (with-custom-display body ...)
(parameterize ([current-output-port new-output-port])
body ...))
This is the general idea of how DrRacket is able to print output to a DrRacket specific repl, rather than your consoles stdout.
1Normally anyway, there are usually ways to break things if you really want to. But that's almost always a bad idea. ;)
Is it possible to update the program code while it is being interpreted by csi, the Chicken Scheme Interpreter? If so, how?
So that I can interactively change part of the code and immediately see the effects of that changes.
For example, suppose I have written the following program:
(define (loop)
(print "Ciao")
(rest 1)
(loop))
(loop)
(assume (rest 1) has the effect of pausing the program for a second).
If I run this program, trough csi, it prints the string "Ciao" every second. If I change the string "Ciao" into something else, for example into "else", and I save the program code file, then csi continue interpreting the old program code, so I continuously see the string "Ciao". I'd like, in this case, that when I save the modified code with the string "Ciao" replaced by "else", csi continue its interpretation job by looking into the modified file, instead of the old file.
So that I obtain as output some "Ciao" followed by some "else": the "else" start to appear when I replace "Ciao" by "else" in the source code.
In general, the answer is "don't". The way you're supposed to use the REPL is by evaluating piecemeal changes against it, then evaluating a function or two to make sure everything went as expected. Part of this approach is structuring your program so that it can be easily tested in pieces, which implies not automatically starting any infinite loops. In the specific case you're asking about, you could instead write
(define (do-stuff)
(print "Ciao"))
(define (main-loop)
(do-stuff)
(rest 1)
(main-loop))
(define (start) (main-loop))
You can now incrementally develop do-stuff, periodically evaluating new versions to your interpreter and calling them to make sure they work, then eventually calling start once you're confident that it's doing the right thing.
You may get mileage out of this similar question asked about Common Lisp.
As an aside, if you were using Common Lisp and SLIME, you could now do more or less what you proposed:
(defun do-stuff ()
(format t "Ciao~%"))
(defun main-loop ()
(loop (progn (do-stuff)
(sleep 1))))
(main-loop)
Starting that up would start printing Ciao on separate lines in your SLIME REPL. If you changed do-stuff to
(defun do-stuff ()
(format t "else~%"))
then hit it with C-c C-c (the default binding for slime-compile-defun), you'd see your SLIME REPL start printing else lines in-flight.
CL-USER> (main-loop)
Ciao
Ciao
Ciao
; compiling (DEFUN DO-STUFF ...)else
else
else
else
else
else
; Evaluation aborted on NIL. User break.
CL-USER>
I'm not sure how to accomplish the same thing in Scheme, but I'm reasonably sure it's possible.
All that being said, you sometimes want to run a program part of which is an infinite loop. A real world example would be while testing a TCP server of some sort. If you're in such a situation, and your desired workflow is
Write file
Run csi my-file.scm
Edit file
Kill csi
Run csi my-file.scm
goto 3
and you basically just want to automate steps 4 through 6, you'll need an external tool to do it for you. Take a look at entr or hsandbox (the latter doesn't have Scheme support out of the box, but it doesn't look like it would be too hard to add).
There is no common way to make a running program check its source for changes but you there seems to be enough feratures available in Chicken to roll your own:
(use posix)
(use srfi-18)
(define (watch-reload! file)
(define (tsleep n)
(thread-sleep! (seconds->time (+ n (time->seconds (current-time))))))
(define (get-time)
(file-modification-time file))
(thread-start!
(lambda ()
(let loop ((filetime '()))
(let ((newtime (get-time)))
(when (not (equal? filetime newtime))
(load file))
(tsleep 10)
(loop newtime))))))
Now all you have to do is to use watch-reload! instead of load and it will check and reload every 10 seconds if the file has been modified.
If you save when the file is not valid scheme it stops working until you call watch-reload! on it again.
It may be that chicken programmers might have a better solution.
I would like to create a command line application that generates jpg images using Quil. I plan to write a couple of generic image processing functions to mix and match (some fn drawing shapes and some manipulating the pixel array).
A simple example of what I would like to do is create one function that draws a circle then a second function that applies a dither algorithm to the pixel array.
(defn draw-circle [x y] ...) ;; e.g. internally uses Quil draw functions.
(defn apply-dither [pixels] ...) ;; e.g. internally uses Quil color functions on the pixels array.
(defn draw []
(draw-circle 100 100)
(apply-dither (pixels))
...)
(defsketch sketch
:draw draw)
What is causing me a bit of grief is all Quil functions seems to only run within a sketch macro. This means that my own functions using the Quil functions internally in turn cannot be directly called (must be called from a draw function triggered by a sketch macro), making debugging and running them individually from the repl harder/impossible.
How do I go about creating and debugging such generic functions? Am I stuck having sketch call functions on my behalf or is there another way?
There is also the possibility that Quil isn't the right tool for my project. I'm considering directly using java / processing classes.
My development environment is Emacs + Cider.
Thanks
I've created a series of quil sketches that run from the command line at https://github.com/rogerallen/qeom
I developed & iterated within emacs+cider and just recompiled the draw function to see the results update live in the sketch window.
I used an atom (defonce dump-image-count (atom 1)) to control when the draw function would save an image:
(defn draw []
...
(when (> #dump-image-count 0)
(save-frame "q007-dump-####.png")
(swap! dump-image-count dec)))
Hope this helps.
I am working through the EOPL Scheme exercises using DrRacket in Windows 7. When I switch from #lang racket to #lang eopl, the output from the definitions pane no longer shows up in the interaction pane. To be clear, as trivial example, running
#lang racket
4
produces
4
> as you would expect. But running
#lang eopl
4produces only
> Is there anything I can do to change this behavior or is there another pane I should be looking at for output? I can, of course, evaluate expressions in the interaction pane and see the output, but this is tedious when I have multiple expressions I want to evaluate multiple times.
A possible workaround: print the value in the definitions pane, so it'll show up in the interactions pane. And use newline to separate lines:
(write 4)
(newline)
(write 2)
> 4
> 2
Of course, it'll be tedious if you want to display many values, but it's an improvement.
Looks like #lang eopl uses #%plain-module-begin, which does not print results, instead of #%module-begin, which does print results.
For a quick way to switch, create the following file with the following contents:
eopl-printing.rkt:
#lang racket
(require (except-in eopl #%module-begin))
(provide (all-from-out eopl))
(provide #%module-begin)
Then use this as the language in another file:
#lang s-exp "eopl-printing.rkt"
1
2
3
produces in my DrRacket:
Welcome to DrRacket, version 5.3.4.6 [3m].
Language: s-exp "eopl-printing.rkt" [custom].
1
2
3
>
CAVEAT: If eopl was hiding the results for a reason, then you may get some spurious output, but I don't know for sure.