No, nope, I'm pretty sure you're evenly matched in your, uh, contest.Quote:
You need some studying to do young'un[/b]
Printable View
No, nope, I'm pretty sure you're evenly matched in your, uh, contest.Quote:
You need some studying to do young'un[/b]
This is a rather pointless, er, "contest." Arby, how does Haskell's performance (when compiled with GHC) compare to native code written in more mainstream languages like C or C++? I never gave Haskell any thought until I read your post, but I just checked out the wiki and it looks pretty interesting.
Kaniaz, Vim users unite! Bellum gerendum contra Emacs est. Actually, I like GNU Emacs but find it painful on my wrists. :?
Pointless, yes... =P oh well.
Well, i'm not really sure about the specifics of why haskell is that efficient since i'm not exactly a computer science graduate but I have a general idea. Heres a demo peice of code just for refernce.
Lets use a factorial function to demonstrate.
in C:
int factorial(x){
y=1
for(i=1,i<=x,i++){
y = y*i
}
return y
}
It takes a few lines, a for loop and some variable calculations. In haskel:
fac 0 = 1
fac n | n > 0 = n * fac (n-1)
A simple recurrsion, no unneeded calculations. Plus, it looks dam sexy fitting all those lines of code into 2 or even 1 line of code:
fac n = if n > 0 then n * fac (n-1) else 1
Anyways, its not more efficient in all cases and i'm really not sure what parts of it are that much better but I worked under a university computer science professor using this language and he said it was more efficient in most cases so i'm assuming he knew what he was talking about XD
Anyways, if you're interested heres a good link
Actually, the C implementation would most likely be recursive, too:
If you really want 1 line:Code:int factorial(int x) {
****if (x == 1 || x == 0)
********return 1;
****else
********return x * factorial(x - 1);
}
Also, your C example doesn't use C syntax :evil:Code:int factorial(int x) {if (x==1||x==0) return 1; else return factorial(x-1)*x;}
Thanks for the link. I'll check it out when I have the time. I'm still curious about how Haskell's performance compares to C/C++, especially for algorithmic code. Maybe Haskell's wiki answers that somewhere.
I started learning C++ because of this topic.
Here's my first program that checks if a number is prime. I'm really angry because I just found out I could've used a modulus operator and made it a lot shorter and faster >:(
#include <iostream>
using namespace std;
float x;
float y;
int z;
int main ();
int composite ()
{
cout << "\nCOMPOSITE";
main ();
}
int main()
{
cout << "\n\n-PRIME-\nEnter a number to determine if it is prime.\n";
cin >> x;
if ( x <= 0 && x != -1 )
composite ();
for ( z = 2; z <= ( x / 2 ); ++z )
{
for ( y = 2; y <= ( x / 2 ); ++y )
{
if ( z == ( x / y ) )
composite ();
}
}
cout << "\nPRIME";
main ();
}
I am making a text-based rpg right now :D It's going to have colored font!
Do you actually call main more than once or something there? In a perfect world, there would be indentation.
Anyway, yeah, first rule of programming. If you find something is beginning to look quite ugly (like your solution and lack of modulus operator above), it's best to think to yourself, "has any other programmer possibly ever tried this before", and usually, more or less, there's a better way to do it.
C is a low-level language, meaning it works the same however you indent it.
I also copied and pasted, and tabs for some reason don't copy and paste into firefox.
And to answer your original question, it's because my compiler requires each function to be defined before it encounters it; in composite(), my compiler doesn't know what main() is yet, so I just have to define it before using it. I wasn't calling it twice.
Also, I feel that if I copy a program, I might as well just download it. You misunderstood my intent with this program; it's to learn, not to create an uber omgI'llfindthelargestprimenumberinzahworld program. Using someone else's code doesn't help you learn. I'm not looking for the best way.
I hope I wasn't too harsh :/ thanks for trying to help.
That's not rightQuote:
C is a low-level language, meaning it works the same however you indent it.
[/b]
First of all C is not a low level language it's a medium level language
A low level language means its closer to machine language unlike a high level language like BASIC which looks more like english
K, well I was going by what a tutorial said, anyway.
You also completely missed the point of what I was saying :/
*realizes this is a nerd topic and leaves* haha now you'll never get my rpg :P
Fine, you'll never get my graphics based one.Quote:
haha now you'll never get my rpg
[/b]
The fact it works the same however you indent it doesn't mean you shouldn't indent ¬_¬
Hey guys,
seems everyone is an expert here... :)
* JAVA (J2EE or rather EE as J2EE is an obsolete term for enterprise java)
* PHP - i think this can be called prog. lang. but someone could call it just scripting lang.
* and of course a lot of stuff around the web (html, css, JS etc...)
Uh, no. Let me clear some of this up for you.
All very true, and the same can be said for C++, but code is always read more times than it is written. The next person to see your code will not be able to see the structure of your program easily with for blocks etc if you do not indent in. The compiler is very loose and ultimately the code loses its indentation when it is compiled, but this does not mean that I can read machine code. Always indent it, it's much easier to read: it's easier to get into the habit now. I understand it Firefox did something weird.Quote:
C is a low-level language, meaning it works the same however you indent it.[/b]
You were. I don't believe your program should compile cleanly (or perhaps even at all). Apart from having recursion in your program that would cause a stack overflow if used too much, you have an illegal definition of composite() - you have said it returns an integer value when it does not return anything. If this compiled, it would have surely made a warning.Quote:
And to answer your original question, it's because my compiler requires each function to be defined before it encounters it; in composite(), my compiler doesn't know what main() is yet, so I just have to define it before using it. I wasn't calling it twice.[/b]
Uh. You can learn a lot from other people's code - I find learning by example one of the best ways to learn something. It's entirely possible to go it alone and end up making something that gets a value in 100 lines of code when there is a much more elegant solution available. I understand the point was to learn something by doing, here, and you've done so, but your program does not run and would not compile.Quote:
Also, I feel that if I copy a program, I might as well just download it. You misunderstood my intent with this program; it's to learn, not to create an uber omgI'llfindthelargestprimenumberinzahworld program. Using someone else's code doesn't help you learn. I'm not looking for the best way.[/b]
A rundown of the problems:
Code:#include <iostream>
using namespace std;
float x;
float y;
int z;
int main ();
int composite ()
{
****cout << "\nCOMPOSITE";
********main ();
}
int main()
{
****cout << "\n\n-PRIME-\nEnter a number to determine if it is prime.\n";
****cin >> x;
****
****if ( x <= 0 && x != -1 )
********composite ();
****for ( z = 2; z <= ( x / 2 ); ++z )
****{
********for ( y = 2; y <= ( x / 2 ); ++y )
********{
************if ( z == ( x / y ) )
****************composite ();
********}
****}
****
****cout << "\nPRIME";
****
****main ();
}
This is fine. Great, in fact, that you're using the STL.Code:#include <iostream>
using namespace std;
Bad news: you're defining global variables. Most people would consider this the Antichrist, these should be defined in a more local scope - main() itself would be fine for is. Moving these declarations into main() would do you a better service. Because you declare them here they are not initialised which is another bad point and could cause your program to crash/be more insecure than it needs to be.Code:float x;
float y;
int z;
You say this function returns an int. It doesn't, a simple mistake, just add return 0; if you want to use this function.Code:int composite ()
{
****cout << "\nCOMPOSITE";
Why are you calling main again? This will cause a stack overflow very quickly if your program is used too much, and you should never call main more than once (contrary to your insistence, you are calling it more than once). Your function should return 0; and then main should loop again. Else the call stack will grow and grow.Code:main();
}
Then in main():
You call main () again when you should be looping it, causing the same problem and making your code nigh on impossible to follow.Code:****main ();
}
So your code does not actually compile, although like we've just gone over, some problems that don't cause compiler errors still cause your code to be very hard to follow. Compiling your program in VC gets me this:
I think that more or less proves my point.Quote:
------ Build started: Project: Prime, Configuration: Debug Win32 ------
Compiling...
main.cpp
c:\users\admin\projects\prime& #092;prime\main.cpp(14) : error C4716: 'composite' : must return a value
c:\users\admin\projects\prime& #092;prime\main.cpp(38) : warning C4717: 'main' : recursive on all control paths, function will cause runtime stack overflow
Build log was saved at "file://c:\Users\Admin\Projects\Prime& #092;Prime\Debug\BuildLog.htm"
Prime - 1 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
[/b]
A better program using your code would look like this:
That works. You will notice:Code:#include <iostream>
using namespace std;
int main()
{
****float x = 0;
****float y = 0;
****int z = 0;
****/* Realise that 'done' will never become true and that you will never actually end this program in this state because you
****** are using cin as the input instead of any other method. Win32 API would use WM_CLOSE messages for example, to set done
****** to true. Therefore the program will loop forever but it since it never calls any other functions it will never crash
****** or overflow. */
****bool done = false, composite = false;
****while ( !done )
****{
********cout << "\n\n-PRIME-\nEnter a number to determine if it is prime.\n";
********cin >> x;
********/* We don't know at the start. So it isn't. */
********composite = false;
****
********if ( x <= 0 && x != -1 )
********{
************cout << "Composite!";
************composite = true;
************/* It's easy enough to break here. */
************continue;
********}
********for ( z = 2; z <= ( x / 2 ); ++z )
********{
************for ( y = 2; y <= ( x / 2 ); ++y )
************{
****************if ( z == ( x / y ) )
****************{
********************cout << "Composite!\n";
********************composite = true;
********************break;
****************}
****************/* Horrible. The way the program is structured requires this. */
****************if ( composite )
********************break;
************}
************/* Also horrific. Don't do this: it's the only way to stop the program from spewing "composite!" over and over
************** again the amount of times the condition is true. */
************if ( composite )
****************break;
********}
****
********if ( !composite )
************cout << "\nPrime!";
****}
****
****/* '0' is the convention for "success" in C++ style programs. It probably does not matter here, but
****** we return it anyway. */
****return 0;
}
1. I removed the composite() function. I didn't see the point on it in this case: it's called only twice and two cout statements could do just as fine. Encapsulating your code is important, but in this case was moot.
2. The code does not cause a stack overflow on runtime, which is important else the program would crash.
3. There is an ugly use of boolean in there to stop the program from spewing out the wrong message. It works, which is great, but the way the program was structured was kind of hard to do any other way (the continue keyword in C++ doesn't support continue all the way up the loop like PHP does). I didn't want to rewrite your entire program as you don't feel you'd learn a lot from that hence the rather nasty looking solution of guard clauses.
4. It compiles.
Either way, that should help.
I'd like to add that even though you claim taking other people's code and perhaps editing it "does not teach you anything", but I must ask you to reconsider.
I've learned PHP and C++ mostly through seeing other people's code and editing it. I've learned how to do new stuff; I could always create (web-)applications with current knowledge, but only to some extent - I couldn't - and still can't - magically sense what functions are used where on a completely new topic, when creating new applications unrelated to anything I've ever done before. Looking at other people's code is the solution here (either that, or tutorials, but I tend to learn more from looking at how other people do it than simply reading a tutorial).
Lol how come ive compiled it and it works then?Quote:
You were. I don't believe your program should compile cleanly (or perhaps even at all). Apart from having recursion in your program that would cause a stack overflow if used too much, you have an illegal definition of composite() - you have said it returns an integer value when it does not return anything. If this compiled, it would have surely made a warning.[/b]
Try actually learning C before you criticize people, loser.
Oh grow up. Your code was invalid and would be choked upon by a compiler, but I won't argue the point (not much debate can come up against those magic compilers). I'm sorry if I spent all that time trying to help you write better code by basically detailing its life story. I won't expect to see much good coming from you until you change the attitude.Quote:
Lol how come ive compiled it and it works then?
Try actually learning C before you criticize people, loser.[/b]
You are in denial. If you actually tried compiling it you would see it works :). Apparently you do not wish to do that because you know I am right, you are wrong.
And what is wrong with using a perfectly respectable compiler? Just because yours might be crap doesn't mean other people can't use good ones :P.
Owned kthxbai. I don't feel like coming back to this nerdy topic so I will leave you to wallow in your self-pity :) I am too cool to be arguing about programming languages lol.
Okay so I'm going to try and compile this motherfucker
This is the funniest topic ever. Thanks M-Cat, even though you won't read this anymore, you've made me laugh harder than I have in a long time.
Okay so it didn't compile in Visual C++ 2005 Express.
M-Cat, what compiler are you using? I really want to get your program to work.