C++ & LISP Hybrid possibilities

Started by
7 comments, last by Sagar_Indurkhya 19 years, 10 months ago
I have heard a lot about LISP, and want to know if it is similar to C++. I want to code a simulation engine, with serious Mathematics, Physics, and AI. I heard LISP has excellent features for AI, but if it is too far from C++, is there any hybrid or such, that would shorten the learning curve. I also am interested in the speed of LISP, and the flexibility. Is it possible to mix and match, like run a LISP DLL from C++ or something? Sagar Indurkhya
Advertisement
Lisp and C++ are completely different in many fundamental ways. Python is probably as close as you''ll get to a hybrid, but to me it seems to fall short of both.

Good Lisp is very fast, but generally just a very little bit slower than equivalent C++. Often that is because Lisp is doing more behind the scenes, but that doesn''t really matter to you if you aren''t using that extra work being done.

It is possible to interface Lisp and C++ with most Common Lisp implementations, but since the Common Lisp standard lacks a foreign function interface specification, how you would interface them will vary from implementation to implementation.
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
As the previous poster said, Lisp is fundamentally different from C++. Lisp is a functional language whereas C++ is a *prepares to shrug of flames* Objet-Oriented and procedural hybrid.

[edited by - xMcBaiNx on May 27, 2004 9:17:50 PM]
I teleported home one night; With Ron and Sid and Meg; Ron stole Meggie's heart away; And I got Sydney's leg. <> I'm blogging, emo style
quote:Original post by Extrarius
Good Lisp is very fast, but generally just a very little bit slower than equivalent C++.


Don''t want to start a flamewar but do you have any evidence of that or is your "generally" limiting to some special cases?

I''d second Python, and reiterate that Lisp and C++ are miles apart.

For your simulation engine you have the choice of using, say, Python, to program everything and then interface to C++ when you need speed. Or to use C++ to program everything and interface to Python/Lisp whenever you need flexibility.

I''d go (and indeed have gone) for python calling C++, it''s far easier that way!
I''ve seen a few fairly thorough benchmarks(sorry, its been a while so I don''t have links, but they were all linked to in gamedev threads) that show Lisp to be close to but slightly behind C++ speed-wise, and in my very limited experience with Lisp I''ve seen nothing to indicate the benchmarks were wrong.

I''m not sure which angle you''re arguing (that Lisp is slower or faster than I suggested), but it makes sense for it to be slightly slower since it is a dynamic langauge and thus more difficult to optimize, but also Lisp is a very old language so more time(relative to newer language like C++ or even C#) has been available for people to figure out language-specific optimization techniques that help Lisp compilers make fast code.
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
Lisp and C++ are so fundamentally different in design that you really can't compare the two with regards to speed.

There are some things that you can do in Lisp that the equivalent functionality in C++ would take much more time to compute (e.g. creating a function on the fly and invoking it... with Lisp this is a part of the language; in C or C++ you would have to either compile to native machine instructions or construct your own interpreter for. Both approaches require more development time and more computation time in C or C++. This is only one example).

There are some things that you can do in C++ that would take longer to compute in Lisp, simply because doing it in Lisp would require more overhead.

A lot of the dichotomy between the two languages is because they are meant for different tasks. This is a generalization, but C (and C++) are primarily intended as a systems programming language. They're meant to access hardware and execute processor instructions in a procedural fashion (with optional object-oriented organization). Lisp is primarily intended as a means of interpreting lists and the patterns found therein. (Lisp itself stands for "List processing") Depending on how you interpret (or compile) your Lisp code, you may also have support for object-oriented organization (using CLOS)... but again it is optional.

The bottom line is, use the right tool for the right task. I myself have found tasks that are better suited to Lisp, and other tasks that I would prefer to code in C++. (I save C for rainy days and Java for last-minute prototypes and Perl for quick-and-dirty text analysis).

There are solutions for interpreting/invoking Lisp code in C and/or C++. There are solutions for interpreting/invoking C or C++ code in Lisp. These can be useful, but can also be very improperly used. Another solution is to pick your language of choice for the base code and then use sockets (or streams... or pipes... or whatever have you) to push data to another module where it can be interpreted as belonging to a different language. This can become quite a headache but allows you to implement any task in your language of choice.

My suggestion to anyone interested is to learn both of these languages (and the others I mentioned as well, oh and ML (or Ocaml) too... and SNOBOL ). But learn them thoroughly before trying to combine them with each other. I cannot stress this enough. Learn the language you use, and know it inside-out. Be able to understand how it operates, the motive behind its behavior, and even the idiosyncracies of how your compiler implements the language before jumping to another language half-cocked. I'm not saying you shouldn't try to learn more than one language at the same time, just that it is wise to know the hows and whys of a language before choosing it over another.

As for learning Lisp, I strongly recommend that any serious programmer attempts to pick it up and use it. It has some wonderful features and some of the things you can do with it will make you think about programming in a completely different way. The downside is that there are few great resources for learning it. I suggest just buying the ANSI book on it by Graham, and/or the Common LISP book by Steele, along with a book geared for AI such as the one by Norvig. I learned the language in an AI class at my University so I had the benefit of specific projects that were tailored to learning the benefits of the language. The Norvig book has some interesting projects in it as well, but it is likely that any text dealing with AI and Lisp will have a few interesting projects.

{whew! I hope I didn't sound like I was on a soapbox, there...}

EDIT: oops! typo in my close-bold markup

[edited by - void* on May 28, 2004 7:55:28 PM]
Greenspun's Tenth Rule of Programming: "Any sufficiently complicated C or Fortran program contains an ad-hoc, informally-specified bug-ridden slow implementation of half of Common Lisp."
Hoi matey,

Scheme is a small dialect of Lisp. I could recommend you might try TinyScheme and embed it into your C++ program.

But still, really, the type of AI in video games, C++ is usually all you need. Most video game AI is FSM (finite state machines) and maybe sometimes a neural net or genetic algorithm, but usually the AI is not very complex. But you could do some cool things with Lisp/Scheme though.

There is practically no Lisp system on Windows I know of that compiles to native code that is free. MIT Scheme works on Windows and I believe it compiles to native code. But the problem with them is I think you need the environment to run your program, you can''t generate a stand-alone executable (I think).

I am sorry to say but AI is the big embarassment of computer science.
quote:Original post by PeterTarkus
There is practically no Lisp system on Windows I know of that compiles to native code that is free. MIT Scheme works on Windows and I believe it compiles to native code. But the problem with them is I think you need the environment to run your program, you can''t generate a stand-alone executable (I think).


http://www.cormanlisp.com/
is for windows and free for personal use and compiles to native code i think.. havent used it much myself but i have seen it recomended by lispers on this board before.


quote:Original post by schlook
quote:Original post by PeterTarkus
There is practically no Lisp system on Windows I know of that compiles to native code that is free. MIT Scheme works on Windows and I believe it compiles to native code. But the problem with them is I think you need the environment to run your program, you can''t generate a stand-alone executable (I think).


http://www.cormanlisp.com/
is for windows and free for personal use and compiles to native code i think.. havent used it much myself but i have seen it recomended by lispers on this board before.

Yup. It is a great environment for Lisp. I believe that Extrarius had linked it up with emacs if that''s your style as well.
If a plant cannot live according to its nature, it dies; so a man.

This topic is closed to new replies.

Advertisement