Author Topic: Mathy/Programmy thing; could use readability feedback  (Read 1845 times)

metroid composite

  • m_ACac
  • Administrator
  • Denizen
  • *
  • Posts: 4375
    • View Profile
Mathy/Programmy thing; could use readability feedback
« on: May 14, 2009, 11:35:18 PM »
Well, a lot of the time I was in Manitoba, I was screwing around with this:

http://www.rpgdl.com/wiki/index.php?title=BBCPP:Main

I think I have a pretty shiny thing going on here, but part of what I find shiny is that I, personally, find the code it produces readable (while I don't find code from the project that inspired it readable).  But that's only my personal perspective; if other people look at the programs and think "that syntax makes the code horrible to read", then the language is not really doing its job.

So...people with some amount of math/programming knowledge: is this stuff readable?  Does it make sense?  Is there something I should change to make it more readable?

Talaysen

  • Ara ara~
  • Administrator
  • Denizen
  • *
  • Posts: 2595
  • Ufufu~
    • View Profile
Re: Mathy/Programmy thing; could use readability feedback
« Reply #1 on: May 15, 2009, 12:45:44 AM »
Yeah, it's pretty easily readable and understandable.

SnowFire

  • DL
  • Denizen
  • *
  • Posts: 4964
    • View Profile
Re: Mathy/Programmy thing; could use readability feedback
« Reply #2 on: May 15, 2009, 02:41:36 AM »
Seems fine to me.  Only thing is that you might consider making the rules for function calls more obvious - I briefly wondered if "add" was a legal function, so you might want to use "foo" or "foo(a, b) or add(a, b)" to make it more obvious it's anything you define.

Also, this is slightly different than the normal rules for (non-Turing Machine) busy beaver?  But I suspect they're roughly equivalent, though.  At least different than the one I did - when we covered Busy Beaver in college, it was with a strict line count and using GOTOs / simple IF THEN (LINE NUMBER) logic.  An if then branch is a form of function invocation, I suppose, hence why it's probably equivalent to most BBs.  Fun stuff, too, as our CS professor invited one of the math profs in to give the lecture that day.

metroid composite

  • m_ACac
  • Administrator
  • Denizen
  • *
  • Posts: 4375
    • View Profile
Re: Mathy/Programmy thing; could use readability feedback
« Reply #3 on: May 15, 2009, 08:33:05 AM »
Also, this is slightly different than the normal rules for (non-Turing Machine) busy beaver?  But I suspect they're roughly equivalent, though.  At least different than the one I did - when we covered Busy Beaver in college, it was with a strict line count and using GOTOs / simple IF THEN (LINE NUMBER) logic.
I...wasn't aware that there were any non Turing-Machine Busy Beaver functions, and never saw BB in a university course, just online websites.

But...yeah, as far as a GOTO approach:
A: GOTOs tend to make for reasonably hard-to-read code; or at least I'm certainly discouraged from using them in professional programming.
B: I come from a math background, so recursive definitions like "a^0 = 1; a^n = a * a^(n-1);" are my natural way of thinking.

Cotigo

  • Jerkface
  • Denizen
  • *
  • Posts: 4176
  • Yoo-hoo, Mr. Tentacle Guy...
    • View Profile
Re: Mathy/Programmy thing; could use readability feedback
« Reply #4 on: May 15, 2009, 03:58:40 PM »
I read that as Mathy/Polygamy thing and was very confused and sort of excited but now I am just disappointed.

metroid composite

  • m_ACac
  • Administrator
  • Denizen
  • *
  • Posts: 4375
    • View Profile
Re: Mathy/Programmy thing; could use readability feedback
« Reply #5 on: May 23, 2009, 06:13:26 PM »
Arg; not enough time to fix wiki before appointment; want to save this somewhere accessible.


==You actually don't even need two variables==
To do this with one variable we're going to use a Gödel construction, where we combine( m, n ) to be 2^m*3^n.  Because of prime factorization, we know that we can always extract both numbers.

The trick is that we don't actually need to ever separate or extract or combine the numbers, which is good because we could never write a Combine(n, m) function since that needs two variables.  Instead:
*PlusOne( m ) becomes TimesTwo( combo )
*PlusOne( n ) becomes TimesThree( combo )
*IfZero( m ) becomes IfZero( DivisibleByTwo( combo ) )
*IfZero( n ) becomes IfZero( DivisibleByThree( combo ) )
*MinusOne( m ) becomes DivideByTwo( combo )
*MinusOne( n ) becomes DivideByThree( combo )

This means we can't go negative, but that's easy enough to simulate if really needed (divisible by 5 can become the m "negative bit", and divisible by 7 can become the n "negative bit").  So...all we have to do is show that we can do all these functions in one variable.  Let's go...

 TimesThree( x )
 {
     ifzero( x )
         return 0
     return PlusOne( PlusOne( PlusOne( TimesThree( MinusOne( x ) ) ) )
 }

 DivisibleByThree( x )
 {
     ifzero( x )
         return PlusOne( 0 )
     ifzero( MinusOne( x ) )
         return 0
     ifzero( MinusOne( MinusOne( x ) ) )
         return 0
     return DivisibleByThree( MinusOne( MinusOne( MinusOne( x ) ) ) )
 }

 DivideByThree( x )
 {
     ifzero( DivisibleByThree( x ) )
     {
         return x //we're not divisible by 3; for the purposes of our positive number system,
                  //0-1 = 0 is acceptable for now.
     }
     ifzero( x )
         return 0
     return PlusOne( DivideByThree( MinusOne( MinusOne( MinusOne( x ) ) ) ) )
 }