Can you do this in FB :)

General discussion for topics related to the FreeBASIC project or its community.
Post Reply
ike
Posts: 387
Joined: Jan 17, 2011 18:59

Can you do this in FB :)

Post by ike »

Code: Select all

unsigned add2(unsigned a, unsigned b) {
  if (a == 0) return b;
  return add2(a-1, b+1);
}
===========================
Imortis
Moderator
Posts: 1925
Joined: Jun 02, 2005 15:10
Location: USA
Contact:

Post by Imortis »

Like so:

Code: Select all

Function Add2(a as UInteger, b as UInteger) as Uniteger
     If a = 0 then return b
     return add2(a-1, b+1)
End Function
If you just meant recursion in general, then the answer is yes.
ike
Posts: 387
Joined: Jan 17, 2011 18:59

Post by ike »

I know. But isn't it funny way to implement function for addition of two positive numbers??

Just look how it works

print Add2(2, 3)

will take much less time then

print Add2(20, 30)
-----------------------------
I have found this in C program and I dont belive it is human written function.

It must be some stupid generator???
Last edited by ike on Nov 02, 2011 0:30, edited 1 time in total.
Gonzo
Posts: 722
Joined: Dec 11, 2005 22:46

Post by Gonzo »

it must be someone with humor...
ike
Posts: 387
Joined: Jan 17, 2011 18:59

Post by ike »

This is funniest function I ever find in a real program ...
gothon
Posts: 225
Joined: Apr 11, 2011 22:22

Post by gothon »

Its actually not a bad recursive definition of addition, if you need to define it in terms of increments and decrements. The obvious thing is of course it makes for a really inefficient computer program, that is on a real processor.

If you use a really simple language that doesn't support addition, this works. Although, you would have to have pretty idealized functional language to support recursion but not addition.
jevans4949
Posts: 1186
Joined: May 08, 2006 21:58
Location: Crewe, England

Post by jevans4949 »

There was a story (urban myth?) of a young programmer working for IBM who figured out an optimisation for a compiler.

He was then told how much he had lost the company in upgrade sales.
maddogg6
Posts: 824
Joined: Dec 07, 2005 22:58
Contact:

Post by maddogg6 »

jevans4949 wrote:There was a story (urban myth?) of a young programmer working for IBM who figured out an optimisation for a compiler.

He was then told how much he had lost the company in upgrade sales.
I say the myth is that its simply 'a story' - as in - it happened *just* once. :)

No need to upgrade hardware if software can be optimized and accomplish the same thing.

When IBM is more a manufacturer than software development firm - ?? - but perhaps thats the crux of my ignorance...??
jevans4949
Posts: 1186
Joined: May 08, 2006 21:58
Location: Crewe, England

Post by jevans4949 »

@maddog6: Sorry, I should have clarified that this was back in the mainframe days when IBM dominated the mainframe market and supplied the software.

Back in the 1970's I worked for a big UK retail bank. One of the senior IT managers came back from an IBM presentation about DB2 relational database, and said we ought to convert from our highly-tailored file access system. The quick assessment was that we would have to triple our hardware - apart from rewriting the software.

Actually, an IBM expert on their sort package came to visit once. "Oh, yeah," the bank man said, "We use it to sort 2 million records every night." IBM man was horrified: "But it was only designed to handle 100K records! You should write your own sort!"

Maybe it's just as well that Microsoft don't sell much hardware.

Today we just have so much power on the desktop that we have to run screensavers to keep our computers from getting bored.
maddogg6
Posts: 824
Joined: Dec 07, 2005 22:58
Contact:

Post by maddogg6 »

jevans4949 wrote:@maddog6: Sorry, I should have clarified that this was back in the mainframe days when IBM dominated the mainframe market and supplied the software.
Yes, back when they actually manufactured those things here (in NY/US IIRC), the 'software' end was so not what IBM wanted to do - but was an 'evil necessity' and why M$ DOS dominated the 'personal' erm 'toy' markets.

Oh the hubris.

edit:

for clarity, my father shared a few stories of his early years at GM and their IBM/EDS calamity (he worked on the first IBM installed for GM in Detroit MI IIRC - or Milford MI proving grounds back in the 60's).

He survived in GM being more of an engineer than a programmer. So this was his sentiment I expressed and not really from my personal experiences and its probably skewed more than a little. ;)
Post Reply