Page 3 of 250

Re: Coding: Fleeting Thoughts

Posted: Tue May 06, 2008 7:28 pm UTC
by bridge
I'm sure about smalltalk

Re: Coding: Fleeting Thoughts

Posted: Tue May 06, 2008 8:11 pm UTC
by btilly
'; DROP DATABASE;-- wrote:Are there any languages with self-modification support?

Yes, lots.

Smalltalk is mostly written in Smalltalk. Lisp macros are immensely powerful. There is a Perl module which lets you write your programs in Latin. Anything you can imagine, and quite a few you can't, are done by some language, somewhere.

Re: Coding: Fleeting Thoughts

Posted: Tue May 06, 2008 8:26 pm UTC
by Rysto
I was under the impression that Lisp macros allow you to change the semantics of the syntax, not modify the code itself.

Re: Coding: Fleeting Thoughts

Posted: Tue May 06, 2008 8:37 pm UTC
by Berengal
Python also allows runtime modification easily:

Code: Select all

>>> import threading
>>> import time
>>> class Foo(threading.Thread):
   def __init__(self):
      threading.Thread.__init__(self)
      self.running = True
   def run(self):
      while self.running:
         print getString()
         time.sleep(4)

         
>>> f = Foo()
>>> getString = lambda:"Hello world!"
>>> f.start()
Hello world!
>>>
Hello world!
Hello world!
getString = lambda:"Goodbye cruel world!"
>>> Goodbye cruel world!
Goodbye cruel world!
f.running = False
>>>

Since it's interpreted dynamically, essentially every session starts blank and modifies its runtime libraries through code, adding new functions and classes as it goes along.

Re: Coding: Fleeting Thoughts

Posted: Tue May 06, 2008 8:47 pm UTC
by ash.gti
Berengal wrote:Since it's interpreted dynamically, essentially every session starts blank and modifies its runtime libraries through code, adding new functions and classes as it goes along.


As does perl, ruby, javascript and almost every scripted language :-P

Rysto wrote:I was under the impression that Lisp macros allow you to change the semantics of the syntax, not modify the code itself.

Lisp macro's can generate code for you. It can allow you to use a 'modified' semantic when writing your code, but fundamentally its just an extremely powerful and complicated preprocess.

a simple example:

Code: Select all

(defmacro triple (x)
    '(+ ~x ~x ~x))

(triple 4)
; evaluates to:
(+ 4 4 4)

; but you can pass it code, and it does a similar thing so say you did...
(triple (Foo 2))

; evaluates to

(+ (Foo 2) (Foo 2) (Foo 2))


It gives you the ability to extend the semantics in a way you want, but its really just a code generation too.

Re: Coding: Fleeting Thoughts

Posted: Tue May 06, 2008 9:10 pm UTC
by EvanED
Code generation isn't really self-modifying code though.

Re: Coding: Fleeting Thoughts

Posted: Tue May 06, 2008 9:19 pm UTC
by cypher
LDJosh wrote:OK. Looks like this board needs a fleeting thoughts thread. - Hammer

While testing some session data handlers, I passed an unset variable to php with the date/time format set. instead of an error, php gives me the 'beginning' of time. so according to php, time began on:

1969 12 31 19:00:00

december 31st, 1969 at 7pm sharp huh?

well let’s see when it will end. Launch outlook, go to tasks. enter a task (like live forever). in the due date, put “forever”.

Seems like time will end on:

Tue 8/31/4500

neat stuff, huh?
This is what I did at work today.


this is an odd thing to point out, but there was an xkcd comic where a character claimed the universe was only 30 years old and anyone claiming to be over 30 is lying. I couldn't find it, but I'm still looking.

Anyhow, that would make the punchline much funnier, because the program claims the universe started in 1969, and 30 years later that would be 1999. It is possible that could have been a reference to that coding line.

g2g. peace.

Re: Coding: Fleeting Thoughts

Posted: Tue May 06, 2008 9:55 pm UTC
by btilly
Rysto wrote:I was under the impression that Lisp macros allow you to change the semantics of the syntax, not modify the code itself.

There are two types of Lisp macros.

Reader macros allow you to rewrite code while it is still being read in. You could in principle use them to create any syntax in the world. In practice there are only a few reader macros that are ever used, like the one that translates 'foo into (QUOTE foo). The reason more aren't use is that reader macros do not play well with each other, and heavy use of reader macros would result in a language that no longer feels like Lisp.

Ordinary macros are different. They can rewrite code in any way you wish. They don't change the syntax, they just rearrange pieces of code. That "just" hides a world of power. Suppose you need a new control structure that isn't in the language? A macro will do it. The power of that statement is hard to communicate to anyone who hasn't ever seen code that does it. If you wish to appreciate Lisp macros, I highly recommend reading On Lisp. (If your budget will afford, I suggest buying the book. But it is up to you.)

Re: Coding: Fleeting Thoughts

Posted: Tue May 06, 2008 10:01 pm UTC
by Berengal
Behold the power of self-modifying code:

Code: Select all

def getNext():
   global getNext
   i = 1
   s = "def getNext2():global getNext; i = %d; s = %s; s2 = %s; s = s %s; exec s; getNext = getNext2; return i"
   s2 = "% (i+1, repr(s), repr(s2), s2)"
   s = s % (i+1, repr(s), repr(s2), s2)
   exec s
   getNext = getNext2
   return i

>>> getNext()
1
>>> getNext()
2
>>> getNext()
3


Now, if only it were useful...

Re: Coding: Fleeting Thoughts

Posted: Wed May 07, 2008 12:47 am UTC
by qbg
In Common Lisp, you have the compiler available at run time, and of course you have EVAL.

Macros, (compiled) closures, etc. gives you a lot of power, so self-modifying code usually isn't needed.

Re: Coding: Fleeting Thoughts

Posted: Wed May 07, 2008 1:00 am UTC
by wing
btilly wrote:There is a Perl module which lets you write your programs in Latin.

That sounds utterly useless.

Re: Coding: Fleeting Thoughts

Posted: Wed May 07, 2008 1:18 am UTC
by Berengal
wing wrote:
btilly wrote:There is a Perl module which lets you write your programs in Latin.

That sounds utterly useless.

From a pragmatic standpoint, yes. It is however interesting in that latin is based on inflection, not position, so parsing is quite different.
For example, in english, "The man ate the sandwich" is quite different from "The sandwich ate the man", but in inflected languages they would be equal as long as the inflections didn't change.
An example of a statement that doesn't change meaning depending on position in english is "'It's hot today' said the man."/"The man said 'it's hot today.'"
Due to the many different inflections in latin, one can reorder the words in a sentence as much one likes without it changing meaning.

Re: Coding: Fleeting Thoughts

Posted: Wed May 07, 2008 1:34 am UTC
by btilly
wing wrote:
btilly wrote:There is a Perl module which lets you write your programs in Latin.

That sounds utterly useless.

I've heard of it being used in production.

Mind you it was used by a programmer who was extremely pissed off, and so he ported his code from Perl to Latin as an FU before he quit. But it did wind up in production.

Re: Coding: Fleeting Thoughts

Posted: Fri May 09, 2008 6:43 pm UTC
by Xanthir
rrwoods wrote:I'm not too familiar with it, but I think Lisp has pretty heavy self-modification support.

As others have said, Lisp doesn't have self-modification per se, at least not in the form that *that* code has where arbitrary instructions later on can change things. That's the devil.

Lisp macros are where the power is at. Lisp gives you the full power of the language at all times - write, compile, and execute time. Macros are functions that run at compile time, and return code. You can *also* call them at runtime, of course, but their power is in allowing you to provide entirely new syntax structures. This is mostly possible because Lisp has next to no typographical syntax - it's all just lists wrapped in parentheses, with the actual 'syntax', as it were, implicit in the definition of things. Frex, a C for loop has a magic syntax that nothing else can ever use, even if you have something that could benefit from a similar structure. On the other hand, the (very basic) analogue for "for" in lisp is dotimes, which is invoked as (dotimes (counter limit returnvar) --code here--). If you think that looks just like a list wrapped in parentheses, you'd be right. It's exactly like every other function and can be easily created and transformed by the standard list-manipulation functions built-in to Lisp.

Fun note: Dotimes is actually a macro! It transforms the code passed to it into a

Code: Select all

(do
    ((var init-val update-val)
    --repeat for more vars)
    ((end-test) return-val)
    --code here--)
statement, which is a lot more complex to code (and much more than you typically need when you're just iterating a variable from zero.

Extra fun note: Do is actually a macro! It transforms the code passed to it into a (tagbody) block of statements, which is *even more* complex to code (and way more than you pretty much ever need, since tagbody is lisp's name for goto). Macros can generate macros which can generate macros. This continues until the macros finally output ordinary functions or values, at which point they get compiled. It's actually possible to implement the entire language as a series of macros over a mere 7 base functions that form the core of lisp, though in practice this isn't done.

Re: Coding: Fleeting Thoughts

Posted: Fri May 09, 2008 8:00 pm UTC
by tehmikey
FT: I wish I would have implemented a random sort on small sets of data when I was first learning to program. Many highschool teachers and professors look at the output before the code. The VASTLY varying efficiencies would prove frustrating to all testers.

Re: Coding: Fleeting Thoughts

Posted: Fri May 09, 2008 8:40 pm UTC
by Berengal
tehmikey wrote:FT: I wish I would have implemented a random sort on small sets of data when I was first learning to program. Many highschool teachers and professors look at the output before the code. The VASTLY varying efficiencies would prove frustrating to all testers.

Oh, indeed. If you're shuffling the whole list, not just swapping two items, between sorted checks, bogosort is O(n!) on average. 7! = 5040. 8! = 40320. 9! = 362880.

Re: Coding: Fleeting Thoughts

Posted: Fri May 09, 2008 11:15 pm UTC
by wing
Berengal wrote:
tehmikey wrote:FT: I wish I would have implemented a random sort on small sets of data when I was first learning to program. Many highschool teachers and professors look at the output before the code. The VASTLY varying efficiencies would prove frustrating to all testers.

Oh, indeed. If you're shuffling the whole list, not just swapping two items, between sorted checks, bogosort is O(n!) on average. 7! = 5040. 8! = 40320. 9! = 362880.

But *QUANTUM* bogosort is O(1) - which is WAY superior to everything else.

[Quantum bogosort is bogosort whewre you randomize once, and then check all possible realities to see which one contains the sorted list, and discard the rest]

Re: Coding: Fleeting Thoughts

Posted: Fri May 09, 2008 11:19 pm UTC
by Rysto
Shouldn't it be O(n)? You have to check whether the list is sorted.

Re: Coding: Fleeting Thoughts

Posted: Sat May 10, 2008 2:18 am UTC
by Xanthir
wing wrote:
Berengal wrote:
tehmikey wrote:FT: I wish I would have implemented a random sort on small sets of data when I was first learning to program. Many highschool teachers and professors look at the output before the code. The VASTLY varying efficiencies would prove frustrating to all testers.

Oh, indeed. If you're shuffling the whole list, not just swapping two items, between sorted checks, bogosort is O(n!) on average. 7! = 5040. 8! = 40320. 9! = 362880.

But *QUANTUM* bogosort is O(1) - which is WAY superior to everything else.

[Quantum bogosort is bogosort whewre you randomize once, and then check all possible realities to see which one contains the sorted list, and discard the rest]

(I know it's a joke, but...) Quantum algorithms don't work like that. They *randomly* select a possible answer from all their combined states. If you've got 10! possibilities and only one's right, that's a pretty small chance of it returning correctly.

The secret is to do statistical tricks that manipulate the frequency space and cancel out as many of the wrong answers as possible. That way your correct answer is a lot more likely to come up. Rerun the algo a few times to get an acceptable bounded probability of being correct, and you're good.

This is why quantum computers aren't magic. Most problems aren't amenable to the sort of frequency-space manipulation of their answer space that is required, or at least, aren't amenable to that in a reasonable amount of time (taking longer to rearrange the space than it would take to just use a classical algorithm is clearly a losing situation).

Re: Coding: Fleeting Thoughts

Posted: Sat May 10, 2008 10:19 pm UTC
by Xeio
A tutorial I've been reading wrote:DirectX only draws triangles that are facing the camera. DirectX defines that triangles facing the camera should be drawn clockwise relative to the camera. If you position the camera on the negative z-axis, the triangle will be defined counter-clockwise relative to the camera, and thus will not be drawn! One way to remove this problem is simply redefining the vertices clockwise (this time clockwise relative to our camera on the negative part of the Z axis)


This is one of those EUREKA! Moments for me, but assuming most graphics engines do this, it explains why when the camera gets stuck in an object it draws only strange parts of the object. It just makes so much sense I kinda went "duh, why didn't I think of that". :mrgreen: Woo learning how things work. :o

In any case learning DirectX is interesting, but I'm only playing around with it a tiny bit at the moment, I'll probably try doing the whole tutorial or something once finals week is over, fun fun. :D

Re: Coding: Fleeting Thoughts

Posted: Sun Jun 15, 2008 7:21 pm UTC
by Dropzone
All hope abandon, ye who enter here. This has to be the most overcomplicated and underdocumented piece of Java API I've ever seen...

Re: Coding: Fleeting Thoughts

Posted: Tue Jun 17, 2008 6:05 am UTC
by '; DROP DATABASE;--
libGD is so much easier to use in PHP than in C. :(

Re: Coding: Fleeting Thoughts

Posted: Wed Jun 18, 2008 2:45 am UTC
by Talion
javascript mutator code:

Code: Select all

function f() {
   alert( 'sheep function says baaa' );
}
function mutate(x) {
  eval('x = '+
  (x+'')
   .replace(/sheep/g, 'zergling')
   .replace(/baaa/g, 'khkhkh')
   .replace(/alert/g, 'document.body.innerHTML = '));
  return x;
}
f = mutate(f);

f();

Re: Coding: Fleeting Thoughts

Posted: Wed Jun 18, 2008 7:03 pm UTC
by 3fj
Dropzone wrote:All hope abandon, ye who enter here. This has to be the most overcomplicated and underdocumented piece of Java API I've ever seen...


Had to look at this at 3am after missing a lecture and having a project to hand in.

I'm not ashamed to admit that i cried.

Re: Coding: Fleeting Thoughts

Posted: Wed Jun 18, 2008 7:33 pm UTC
by sparkyb
Dropzone wrote:All hope abandon, ye who enter here. This has to be the most overcomplicated and underdocumented piece of Java API I've ever seen...


Then apparently you've never tried to do anything significant with JMF (Java Media Framework).

Re: Coding: Fleeting Thoughts

Posted: Mon Jun 23, 2008 5:02 pm UTC
by Marz
Befunge is an esoteric language allowing self-modifying code.
All code exists in Fungespace, from which you may get, put, and interpret.
But it's more for fun than actual programming. It was interesting to write an Interpreter for, though.

Re: Coding: Fleeting Thoughts

Posted: Mon Jun 23, 2008 11:47 pm UTC
by Berengal
Marz wrote:Befunge is an esoteric language allowing self-modifying code.
All code exists in Fungespace, from which you may get, put, and interpret.
But it's more for fun than actual programming. It was interesting to write an Interpreter for, though.

Now write a compiler.

Re: Coding: Fleeting Thoughts

Posted: Wed Jul 02, 2008 2:04 am UTC
by The Hyphenator
Goddammit, I just spent an hour on a tic-tac-toe AI. It took me half that time to just get my shit together and realize my current approach wasn't working. Now I finally got really started, although it's not even close to being close to done yet. Why can I never do things right the first time?

*bangs head against wall*

Re: Coding: Fleeting Thoughts

Posted: Wed Jul 02, 2008 3:01 am UTC
by Berengal
Would you really need an AI though? Isn't that game small enough to brute-force a game-tree?

Of course, writing an AI can be fun too.

Re: Coding: Fleeting Thoughts

Posted: Mon Jul 07, 2008 3:20 pm UTC
by The Hyphenator
Berengal wrote:Would you really need an AI though? Isn't that game small enough to brute-force a game-tree?

Of course, writing an AI can be fun too.

Well, I've never written either an AI or a game tree before (except for a really basic AI in one of my games), so I just thought an AI would be a cool thing to try. :oops:

*wikis game tree*

Since my AI is still having... problems, I'll probably do that instead.

Re: Coding: Fleeting Thoughts

Posted: Fri Jul 11, 2008 8:01 pm UTC
by Ptolom
why is it so goddamn hard to write a simple tile based game engine? It looked so simple, just draw the tiles on a grid and then feed back some data when they're walked on. Now I'm grappling with a program to read through huge text files full of cryptic lists of letters. Why is it never simple

Re: Coding: Fleeting Thoughts

Posted: Sat Jul 12, 2008 2:00 am UTC
by Xeio
Ptolom wrote:why is it so goddamn hard to write a simple tile based game engine? It looked so simple, just draw the tiles on a grid and then feed back some data when they're walked on. Now I'm grappling with a program to read through huge text files full of cryptic lists of letters. Why is it never simple

Doesn't seem that hard. I wrote a simple tile based engine (I assume we're talking 2D?). In retrospect though, I didn't make it very modular, nor comment the code, but I had the aspiration to code a game as my first 'big' project.

I never did actually work on the 'game' part, just the engine, quite fun. Its filed under my "technically finished, but I never coded some things that would be useful to actually finish making a game out of it" folder. I even had a spiffy map editor. :mrgreen:

Maybe try breaking it into smaller pieces? Like, don't try to get all the features running at once, go for just the map tiles first (should be fairly easy to just draw the correct image at each position). Then work up I guess? :X

Re: Coding: Fleeting Thoughts

Posted: Mon Jul 14, 2008 12:06 am UTC
by The Hyphenator
It worked the first time?

It worked the first time?

So I was just working on a program with a function with lots of bitwise operators, pointers, etc, and I expected to see a huge list of complaints on compile, but it compiled fine. Then I thought it would definitely return a memory error on runtime.

But it didn't! It worked perfectly! :shock:

Middle-of-reply edit: I just tried it with different inputs, and it still worked. I feel so proud. :D

What I was trying to do, if you care:
Spoiler:
Make a new ultra-optimized Game of Life implementation. I saved each of the 16 combinations of four cells in an image, so it would only have to display one-fourth as many surfaces (each surface has four cells, instead of one). The display function takes an 2-d array of bytes containing the cell data, one bit per cell. For each byte, the upper and lower four bits are converted into clip rectangle coordinates in the image, then that part of the image is displayed in the right place.

NP will tell me some simple way to optimize it further that I missed. :)

Re: Coding: Fleeting Thoughts

Posted: Mon Jul 14, 2008 12:40 am UTC
by azredwing
Fleeting thought:

I know C, Perl, and assembly. Until now, my style convention has been that all_variables_should_be_underscored.

I am now teaching myself Java, and the Java naming convention is that allVariablesShouldHaveCapitalLettersAndNoUnderscores.

Since it's all style, does it really matter if I kept the style of using underscores to separate words, rather than use capital letters? And how much would that piss off other Java programmers who looked at my code if I did?

Re: Coding: Fleeting Thoughts

Posted: Mon Jul 14, 2008 7:20 am UTC
by Sc4Freak
It's not a big deal, but it's best to stay idiomatic. For example, my style when writing C# is different to when I write C++ - in C# I generally follow the Microsoft guidelines because that's generally what a lot of C# code is written in. I assume the same would be for Java: when in Rome, do as the Romans do.

Re: Coding: Fleeting Thoughts

Posted: Tue Jul 15, 2008 4:29 pm UTC
by xulaus
The dev team for PHP has a KISS policy. heh.

Re: Coding: Fleeting Thoughts

Posted: Wed Jul 16, 2008 11:17 am UTC
by Duck
Ptolom wrote:why is it so goddamn hard to write a simple tile based game engine? It looked so simple, just draw the tiles on a grid and then feed back some data when they're walked on. Now I'm grappling with a program to read through huge text files full of cryptic lists of letters. Why is it never simple


What part are you having trouble with?
In my experience, writing and reading lists of letters and numbers to and from files is something that most languages tend to be fairly good at.

Re: Coding: Fleeting Thoughts

Posted: Wed Jul 16, 2008 9:25 pm UTC
by You, sir, name?
'; DROP DATABASE;-- wrote:Interesting/terrifying idea: self-modifying C. A statement that says "change this other statement to this". Something like:

Code: Select all

for(int i=0; i<5; i++)
{
   fooequalsone:
   foo = 1;
   if(i > bar) break;
   change fooequalsone foo = 0;
}



Sort of like static in C-like languages. Oh, what shenanigans that keyword can be up to.

Code: Select all

void foo() {
        for(static int i = 0; i < 5; i++) printf("%d", i);
}

int main() {
        for(;;) foo();
        return 0;
}


When run, produces

01234[nothing]

:-D

Re: Coding: Fleeting Thoughts

Posted: Wed Jul 16, 2008 9:59 pm UTC
by headprogrammingczar
It HURTS!

Re: Coding: Fleeting Thoughts

Posted: Thu Jul 31, 2008 10:18 pm UTC
by The Hyphenator
Oh my god. Python rules.

This is the thought that has been running through my head repeatedly since I coded my first Python program yesterday morning. It really is JUST SO EASY. I've only used it for two days, and I already think it might be my favorite language.