AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

did you know that ...

 
Reply to topic    AutoHotkey Community Forum Index -> General Chat
View previous topic :: View next topic  
Author Message
heresy



Joined: 11 Mar 2008
Posts: 291

PostPosted: Tue Aug 05, 2008 11:13 am    Post subject: did you know that ... Reply with quote

you were already coding C?

factorial
Code:
; C Language
int factorial(int x)
{
    if (x == 0) {
        return 1;
    } else {
        return x * factorial(x - 1);
    }
}

Code:
; AutoHotkey
factorial(x)
{
    if (x == 0) {
        return 1
    } else {
        return x * factorial(x - 1)
    }
}


fibonacci
Code:
; C Language
unsigned int fibonacci(unsigned int n)
{
    if ((n == 0) || (n == 1))
        return n;
    else
        return fibonacci(n-1) + fibonacci(n-2);
}

Code:
; AutoHotkey
fibonacci(n)
{
    if ((n == 0) || (n == 1))
        return n
    else
        return fibonacci(n-1) + fibonacci(n-2)   
}


even one-liner fibonacci
Code:
; C Language
unsigned int fib(unsigned int n)
{
    return n < 2 ? n : fib(n-1) + fib(n-2);
}

Code:
; AutoHotkey
fib(n)
{
    return n < 2 ? n : fib(n-1) + fib(n-2)
}

_________________
Easy WinAPI - Dive into Windows API World
Benchmark your AutoHotkey skills at PlayAHK.com
Back to top
View user's profile Send private message
Krogdor



Joined: 18 Apr 2008
Posts: 1390
Location: The Interwebs

PostPosted: Tue Aug 05, 2008 6:43 pm    Post subject: Reply with quote

They become farther apart when doing things more complicated than math operations, though...
Or so I think?
Back to top
View user's profile Send private message AIM Address
Red Hat Dude
Guest





PostPosted: Wed Aug 06, 2008 9:50 pm    Post subject: Reply with quote

I remember my first attempts at learning c++ were complete failures. I could do some basic stuff (the most complicated thing I ever wrote was a hex/number converter).
I just decided to start learning Ruby today. Popular in Japan, I hear.

Sample code from a command-line gallon to liter converter in C++ (that I wrote):
Code:

#include <iostream.h>
#include <string>

int main()
{
  int doStuff;
  float gallonAmountInput, gallonAmount;
  float literAmountInput, literAmount;
  char gUnit, mUnit;
  string gUnitWord, mUnitWord;
  cout << "Enter 1 to convert gallons to liters, 2 for liters to gallons, 0 to quit: ";
  cin >> doStuff;

 while (doStuff != 0)
 {

  while (doStuff == 1)
  {

  // repeat this until user enters a valid unit
  cout << "Enter units of measure. Units include: " << endl << "g for gallons" << endl;
  cout << "q for quarts" << endl << "p for pints" << endl << "c for cups" << endl << "o for ounces" << endl;
  cin >> gUnit;

  cout << "Enter amount: " ;
  cin >> gallonAmountInput;
  gallonAmount = gallonAmountInput;
  switch (gUnit)
  {
  case 'g' :
  gUnitWord = "gallons";
  break;
  case 'q' :
  gallonAmount /= 4;
  gUnitWord = "quarts";
  break;
  case 'p' :
  gallonAmount /= 8;
  gUnitWord = "pints";
  break;
  case 'c' :
  gallonAmount /= 16;
  gUnitWord = "cups";
  break;
  case 'o' :
  gallonAmount /= 128;
  gUnitWord = "ounces";
  break;
  }

  literAmount = gallonAmount * 3.785;
  cout << gallonAmountInput << " " << gUnitWord << " is " << literAmount << " liters. " << endl;
  cout << "Enter 1 to convert gallons to liters, 2 for liters to gallons, 0 to quit: ";
  cin >> doStuff;
  }

  while (doStuff == 2)
  {

  cout << "1 for Liters, 2 for Milliliters (mL): ";
  cin >> mUnit;

  cout << "Enter amount: " ;
  cin >> literAmountInput;
  literAmount = literAmountInput;
  switch (mUnit)
  {
  case '1' :
  mUnitWord = "liters";
  break;
  case '2' :
  mUnitWord = "milliliters";
  literAmount /= 1000;
  break;
  }
  gallonAmount = literAmount / 3.785;
  cout << literAmountInput << " " << mUnitWord << " is " << gallonAmount << " gallons. " << endl;

  cout << "Enter 1 to convert gallons to liters, 2 for liters to gallons, 0 to quit: ";
  cin >> doStuff;
  }

  if (doStuff == 0)
  {
  return 0;
  }
 }

}
Back to top
Dragonscloud



Joined: 16 Jul 2005
Posts: 96

PostPosted: Thu Aug 21, 2008 12:34 pm    Post subject: Reply with quote

I liked the structure of Ruby, and the scripts perform pretty well, but when I compiled them they ran very slowly in comparison to AHK, or even VBA.
_________________
“yields falsehood when preceded by its quotation” yields falsehood when preceded by its quotation.
Back to top
View user's profile Send private message
Red Hat Dude
Guest





PostPosted: Thu Aug 21, 2008 11:08 pm    Post subject: Reply with quote

Heh, I didn't care for the syntax of Ruby. I still plan on learning it, at least a little, but I'm gonna take a look at Python.
Both Ruby and Python are pretty high-level languages, which means they have less complicated code than a C++ program, but may run slower...
Back to top
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> General Chat All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group