3
$\begingroup$

My situation is that I write Racket/Scheme code to run some simulation. Then I'd like Mathematica to call Racket to run that code file.

For example: the code file is named foo.rkt which is a module and it provides the command `hello with some arguments as inputs.

I can run this command in a Terminal like this:

~@laptop$: racket -t foo.rkt -l racket -e '(hello ....)'

(racket will require the module foo.rkt, then it requires the library racket/main, then finally will evaluate the function (hello x y z). at the end it exports data and plot the data)

I'd like to ask how can I make this happen in Mathematica, as I heard that Mathematica can handle external program by giving input then capture the output.

Thank you so much, chi

$\endgroup$
  • $\begingroup$ Welcome to Mathematica.SE! I hope you will become a regular contributor. To get started, 1) take the introductory tour now, 2) when you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge, 3) remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign, and 4) give help too, by answering questions in your areas of expertise. $\endgroup$ – bbgodfrey Oct 11 '15 at 20:12
  • $\begingroup$ Look up RunProcess and related functions. $\endgroup$ – Szabolcs Oct 11 '15 at 20:57
2
$\begingroup$

Pretty easy just try the below code

Run["racket -t foo.rkt -l racket -e '(hello ....)'"]

I don't have mathematica on this system to check it for you right now though. the trick is to run racket through kernel try this code and let us know what happened it may need bit of tweaking.

remember if you can't capture the output you have to send it to a file and read it from there, you can also give it the input through a file by using '<' and '>', for example

racket > itest.txt

this will save the output in the following text file this is the best way specially if you have a long output file.

$\endgroup$
  • 1
    $\begingroup$ With RunProcess you can read/write standard input/output directly, without the need to go through files. Run is the old way, which doesn't support this. $\endgroup$ – Szabolcs Oct 12 '15 at 8:57
  • $\begingroup$ Yes, still it doesn't always work! specially if you have a long output. $\endgroup$ – Raymond Ghaffarian Shirazi Oct 12 '15 at 10:35
  • $\begingroup$ sorry but there is a problem with the directory path. it'd be easy if it's foo.rkt but matha initiates in a different directory from the one holding foo.rkt i already add the directory to $Path but it still says File not Found during RunProcess $\endgroup$ – Linh Chi Nguyen Oct 12 '15 at 14:46
  • $\begingroup$ as I mentioned there will be few tweaking needed you are using mathematica kernel not the shell you usually use. So, please kindly write down the exact errors who you get. also your OS $\endgroup$ – Raymond Ghaffarian Shirazi Oct 12 '15 at 16:46
  • 1
    $\begingroup$ Thank you, finally i know how to do this, i just set the current directory to the notebook directory SetDirectory[NotebookDirectory[]] then Run[racket -tm foo.rkt]. I don't know why but RunProcess still says File not Found during RunProcess. $\endgroup$ – Linh Chi Nguyen Dec 30 '15 at 16:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.