Mathematica supports an exact list of compilable functions:

They are listed here: List of compilable functions.

However, I would need to add the following functions: ToExpression, StringFreeQ, StringReplace, BinaryRead and BinaryWrite...

Is it possible?

I know that BinaryRead and BinaryWrite have simple C++ equivalents but how can I add this functionality to the Compile function?

Furthermore, Compile only accepts Integer, Floats and Lists and Nested Lists as input. Can it be modified to accept Strings?

Could reimplementing the Compile Function work?

  • 2
    $\begingroup$ These functions can be used in Compile and can be part of a compiled functions. However, when they're run, the byte-code interpreter will fall back to standard evaluation which mean that these functions won't get a performance boost from compilation. The rest of your program will get a performance icrease, just not these particular functions. $\endgroup$
    – Szabolcs
    Commented May 29, 2014 at 18:48
  • $\begingroup$ Does the standard evaluation call the MathKernel or can the generated code be used standalone? Can I also add support for strings or would a string be a Integer list representing ASCII codes? $\endgroup$
    – user13675
    Commented May 29, 2014 at 20:25
  • $\begingroup$ It calls MathKernel. I'm not sure how exactly LibraryLink functions interact with compiled functions. It may be possible to implement these I/O functions using LibraryLink to make it possible to integrate them into a standalone program. Unfortunately I don't have experience translating Mma into standalone C code (though the docs suggest it's possible to an extent), so I'll leave this for others to answer ... $\endgroup$
    – Szabolcs
    Commented May 29, 2014 at 20:27

1 Answer 1


As far as I know there is no way to extend the Compile system in the way you describe, but you can often work around specific problems. For example, for handling strings you can write an outer function that converts strings to a packed vector of integers using ToCharacterCode, then pass this to your compiled function. However, I would encourage you not to take this approach as the string tools already make use of compiled libraries (including PCRE).


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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