0

I have a huge lisp function that I want to use in my c++ application. This function was generated using a decision tree making algorithm.

I don't have a good idea on how to easily incorporate it into my code. I don't want to use any external libraries, and I don't want to make classes with trees and leaves from it. Also, I want to make it easy to load, that is why I first thought about compiling it in a big if-then function.

If anybody has any idea how to make this compilable in a beautiful way, please tell me. If you don't know what I want to do, or don't have any ideas, then please just don't post anything.

This is a sample of the lisp function. It is around 10.000 lines in full.

((prev_is_pause is 0)
 ((prev_prev_gender is d)
  ((houses_from_month_start < 1.9)
   ((houses_from_month_start < 0.1)
    ((customer_numsegs < 3.6)
     ((customers_from_company_start < 6)
      ((prev_jobtype is n)
       ((next_hairtype is -)
        ((0.0332597 0.109037))
        ((0.0535314 0.143047)))
       ((called_houses_from_month_end < 3.5)
        ((next_next_single is +)
         ((0.0682613 0.200642))
         ((month_numhouses < 8.3)
          ((0.0631857 0.11954))
          ((0.0737018 0.165414))))
        ((0.0442889 0.225281))))
      ((0.075459 0.110669)))
     ((next_garden is 2)
      ((0.0726842 0.152881))
      ((prev_jobtype is n)
       ((0.0458975 0.12033))
       ((next_customerbegin_jobtype is s)
        ((0.0246754 0.0992865))
        ((prev_prev_talks is aI)
         ((0.0240179 0.0708011))
         ((0.0238049 0.0894625)))))))
    ((prev_birthplace is a)
     ((next_invoice is ".")
      ((company_numcustomers < 13)
       ((called_houses_from_month_end < 2.4)
        ((next_talks is t)
         ((0.0586338 0.13392))
tmighty
  • 10,734
  • 21
  • 104
  • 218
  • 3
    Looks like you want a decision tree or perhaps a state machine. Bottom line: nearly all of what you're showing here should end up as data, with a small "engine" to check and return values. – Jerry Coffin Apr 06 '13 at 17:12
  • @Jerry Coffin Yes, that is true. It is a decision tree. Thank you for your suggestions. – tmighty Apr 06 '13 at 17:35
  • can you not just get the source generator to spit out C++ instead of lisp? – dsm Apr 06 '13 at 20:21

2 Answers2

1

You have two options:

  • Use Lisp as a scripting language embedded in your C++ app, and then run your function as a script. See this answer to find some C++ libraries for such a task.
  • Totally translate your lisp function in C++. Apparently, you have put data and code in the same function. You should separate it, put the data in a file that you will load, and then make the algorithm more clear. Don't forget to separate the algorithm in several distinct functions... Also, a 10000 lines function is a pain to maintain, so even if you're not translating it to C++, you should break it as said before. If you know exactly what does the function, it should not be too difficult. You can use some C++11 features to emulate a sort-of functionnal language.
Community
  • 1
  • 1
Synxis
  • 9,236
  • 2
  • 42
  • 64
  • Thank you. I don't have to write the code by hand. I will parse the data and transform/refactor it. Is your answer still valid then? – tmighty Apr 06 '13 at 17:32
  • You you stay with lisp, yes, because of the link to the lisp-C++ libs. What do you mean by "transform/refactor" the data ? – Synxis Apr 06 '13 at 17:34
  • I could easily turn this into anything else (if-then loop, classes), but I care about serialization/loading. This becomes relatively slow when doing this with classes. Just to make sure... the lisp function above is just a big if-then. Right? I was 100% sure until you wrote your last comment. – tmighty Apr 06 '13 at 17:46
  • 1
    I don't know lisp, but I think a if is `(if condition success fail)`. I don't see any `if` here... Can you be more precise on the problem you're trying to solve (ie, not the programming problem: what is the algorithm, why such conditions, etc...). Also, don't forget to say in your question that your function is automatically generated (which I learnt by reading another of your questions). – Synxis Apr 06 '13 at 17:51
  • Yes, you are right. The data is like a big if-then with "sucess" and "fail". Each indention stands for an if. Thank you for your help so far! – tmighty Apr 06 '13 at 18:08
0

Yet another option is using the Callback mechanism of CFFI:

http://common-lisp.net/project/cffi/manual/html_node/defcallback.html#defcallback

But this will change the way how you look at your application.

user2240219
  • 166
  • 1
  • 2
  • 2