views:

171

answers:

4

What are my options in terms of a fast functional language for library use in a cross-platform Qt C++ application?

It seems almost all languages (functional or not) have some way of calling C/C++ code in an easy manner. I'd like to go the other way around - write an application in Qt using C++ for stateful business logic, GUIs and stuff but drop down and use a functional language for the core calculation library.

Which ones are easy to use in this manner? Can for instance OCaml code be compiled into a static library and then consumed by a C++ app?

Thanks, Rickard

+2  A: 

AutoCAD uses AutoLisp so my suggestion would be Lisp.

Iulian Şerbănoiu
I have too little experience with Lisp but I wonder if the Lisp compiler can produce c-consumable bytecode so I simply can call functions in the library? I want to avoid having to manage an interpreter embedded in the application (like when one embeds Python in a C app and have to fiddle around with reference counting and PyObject*s just to call a function).
Rickard
@IulianŞerbănoiu: Yes, but AutoLisp has been created specifically to be embedded into AutoCAD. This does not mean that all Lisps automatically have the property that their functions can be easily called from C++ code. Note that he's not trying to embed a language into his C++ app, but to call compiled library code written in another language from his app.
sepp2k
+4  A: 

Haskell has this capability, though the interaction with Qt/qmake and your build process with ghc may take a little trickery to get working:

http://www.haskell.org/haskellwiki/Calling_Haskell_from_C

There is also a project called HaskellDirect which seems similar to your purpose as well:

http://www.haskell.org/hdirect/

d.w.
I'm tempted to go down this road. It's a small non-commercial project so not much is at risk except my sparetime. I'm assuming that the haskell runtime won't have any problems with concurrency and that I can call into the library from several threads at the same time?I'll probably accept this answer in a little while even though maybe this was a bit of a subjective question with no clear-cut "correct" answer.
Rickard
+1  A: 

I'd be tempted to check out qtHaskell and to do the whole thing in Haskell. My opinion is based on Don Stewart's remarkable success doing xmonad in Haskell.

Norman Ramsey
It is tempting but I want to rely on ports of libraries (like qt) as little as possible. Certain things are also so very easy to do with classes in C++ when it comes to state-keeping logic.Thanks anyway for your answer - I'll certainly consider the possibility.
Rickard
+1  A: 

Lisp and Haskell are excellent functional languages but if we consider the ease of binding C/C++ code along with the language, I'd recommend lua.

It is extremely straightforward to bind C functions to lua right off the bat, the interpreter is super compact and easy library to build, it's among the fastest scripting languages out there, and, with luabind, you can easily bind C++ classes, template instantiations, etc. I've had to do bindings for numerous scripting languages in the past and I've never found one that's quite as straightforward as lua. It's also supported with swig if you prefer to bind things through swig which will allow your application to support multiple scripting languages.

From a pure language point of view, the meta-feature/metaprogramming aspect of lua (comparable to lisp) makes it very easy to support all kinds of programming paradigms, though I personally find it best suited for functional programming. It's extremely customizable and well-suited for embedded use.

However, since you are already using qt, qtHaskell might be a nice choice to consider as well.

Maybe Lua is a much better choice for stuff like this but I know the hassles that comes with embedding and running python code from a C/C++ app. It is so much more work to do than just initialize some runtime and then call functions. I'll take a look at the C-api in the Lua documentation though. I heard great things about the speed of Lua so it certainly is interesting.Thanks for you answer.
Rickard