Have you written a program to write a program?

General chat, humour, riddles, logic/lateral/word puzzles...
Post Reply
Whovian
Posts: 4
Joined: Mon Jun 11, 2012 2:40 am

Have you written a program to write a program?

Post by Whovian »

As the title asks, have you ever written a program in one language to write a program in another language, maybe to use a clever solution in one language to write a brute force program in the other language that uses the second language's useful features? I seem to remember writing a C++ program to cleverly add a bunch of large numbers by using Python's lack of restrictions on integer size, since writing the Python script manually would've taken forever.

wrongrook
Posts: 403
Joined: Sat Oct 17, 2009 10:39 pm

Re: Have you written a program to write a program?

Post by wrongrook »

I use this kind of metaprogramming quite often when I need to write in hardware description languages (such as Verilog or VHDL) because these languages are relatively verbose. However, my preferred direction is opposite to yours - I tend to write Python code to generate the RTL output.

I find my metaprograms to be very compact, but quite hard to read (they have a strange mix of strings of RTL code and Python code), so actually my favorite approach is to invent a task specific language and then write a Python compiler to convert this into the target language.

ThomasH
Posts: 117
Joined: Sun Mar 26, 2006 8:41 am
Location: Berlin, Germany

Re: Have you written a program to write a program?

Post by ThomasH »

Yes, I did sometimes: I wrote a program in Pascal to write a program in Pascal. It was the case, when I had to use a lot of for statements with very similar body (and did not find the recursion to do the same).

User avatar
elendiastarman
Posts: 410
Joined: Sat Dec 22, 2007 8:15 pm

Re: Have you written a program to write a program?

Post by elendiastarman »

I've done this at least once. Python is great at number-crunching and the like, but not graphics whereas Blitz Basic/3D is essentially the opposite. So, one time, I wrote a Python program that generated a specified number of for-loops in Blitz Basic/3D code. :P
Want some
3.14159265358979323846264338327950288419716939937510
58209749445923078164062862089986280348253421170679...?
Image

Ahaanomegas
Posts: 10
Joined: Mon Jun 18, 2012 1:14 am

Re: Have you written a program to write a program?

Post by Ahaanomegas »

Obviously, when we write functions, it can be considered as "writing a program" for a program, true?
Image

LarryBlake
Posts: 100
Joined: Sat Aug 29, 2009 8:49 pm

Re: Have you written a program to write a program?

Post by LarryBlake »

I've done these:

1. In an ASP program, read a database to get values for dynamically building a menu. As it reads, the ASP creates the JavaScript to construct the menu.

2. Programs that operate on programs as data. For example, to indent. I've also looked for specific code and inserted additional logic when encountered (no, not for a virus).

3. Often I write in some other language to create SQL statements dynamically, and sometimes more complex SQL logic.

4. Programs to generate Excel spreadsheets, including complex Excel formulas.

I feel like I have written programs to build entire other programs, but I can't think of one right now.
Image

fufuf
Posts: 2
Joined: Wed Jul 25, 2012 8:23 am

Re: Have you written a program to write a program?

Post by fufuf »

Oddly enough the most satisfying problem for me is one I couldn't solve: 361, the Thue-Morse problem. I still have no idea how to solve it, but I learned so much trying to solve it that I really enjoyed the dozen of hours I spent on it


Edit by hk: spam link removed.
-----------------------------

User avatar
Francky
Posts: 90
Joined: Sat May 07, 2011 3:49 pm
Location: South of France

Re: Have you written a program to write a program?

Post by Francky »

I wrote a program in python in order to unroll my 210-whell sieve of Eratosthenes, the result was a thousands line program, that is my fastest sieve in python for bound greater than 10⁹. In C, my fastest sieve is not a 210-whell.

I wrote a program in python in order to write (in C or in python) a function that solve recursive sequence of order k (for each k<18), I used it with success here.
ImageEntia non sunt multiplicanda praeter necessitatem

Post Reply