For Programmers: Free Programming Magazines  


Home > Archive > Cobol > January 2006 > Re: free implementation? factorial?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: free implementation? factorial?
Nicolas Neuss

2006-01-10, 7:55 am

Louis Krupp <lkrupp@pssw.nospam.com.invalid> writes:

> I've never used TinyCobol, but it might be worth a try. I think an
> iterative factorial implementation might be easier to follow than a
> recursive routine. This would look something like:
>
> move 1 to factorial.
> perform varying t from 2 to number
> multiply factorial by t
> end-perform.
>
> Louis


Thanks, I'll try this, too.

Nicolas.
Alain Reymond

2006-01-10, 7:55 am

Nicolas,

Try to use the recursive posssibilities of modern Cobol. This is good
for teaching and showing that Cobol is no more an amount of go to's in
spaghetti code (troll)...

Here is an example (works under Micro Focus Cobol):

$set sourceformat"free"
identification division.
program-id. factorial.
data division.
working-storage section.
1 m pic 9(4).
local-storage section.
1 n pic 9(4).
linkage section.
1 aNumber pic 9(4).
1 fact pic 9(18). *> The longest you can
procedure division using aNumber fact.
move aNumber to n
if n>0 then
compute m = n - 1
call "factorial" using by content m by reference fact
compute fact = n * fact
else
compute fact = 1
end-if
exit program
Peter Lacey

2006-01-10, 9:55 pm

Alain Reymond wrote:
>
> Nicolas,
>
> Try to use the recursive posssibilities of modern Cobol. This is good
> for teaching and showing that Cobol is no more an amount of go to's in
> spaghetti code (troll)...
>

Why teach the more complicated method first? This isn't a case where
using recursion is preferable. Also (troll-reply) an instructor
shouldn't get involved in religious wars unless that happens to be the
course subject.

PL
Alain Reymond

2006-01-10, 9:55 pm

Peter Lacey a écrit :
> Alain Reymond wrote:
>
>
> Why teach the more complicated method first? This isn't a case where
> using recursion is preferable. Also (troll-reply) an instructor
> shouldn't get involved in religious wars unless that happens to be the
> course subject.
>
> PL


I should have added a smile!

the fact is that factorial is usualy the first approach to recursion
when teached to students.
As the other solution is quite easy, the aim was to show that an elegant
(I hope so!) solution also exists in Cobol using the same concepts as in
C or Pascal or java which probably are more teached to students than Cobol.

Regards.

Alain
Peter Lacey

2006-01-10, 9:55 pm

Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable
Lines: 57
Date: Tue, 10 Jan 2006 21:05:58 -0600
NNTP-Posting-Host: 142.161.11.99
X-Trace: fe14.lga 1136948742 142.161.11.99 (Tue, 10 Jan 2006 20:05:42 MST)
NNTP-Posting-Date: Tue, 10 Jan 2006 20:05:42 MST
Xref: number1.nntp.dca.giganews.com comp.lang.cobol:155176

Alain Reymond wrote:
> =


> Peter Lacey a =E9crit :
[color=darkred]
n[color=darkred]
e[color=darkred]
e[color=darkred]
> =


> I should have added a smile!
> =


> the fact is that factorial is usualy the first approach to recursion
> when teached to students.


If you say so. =


> As the other solution is quite easy, the aim was to show that an elegan=

t
> (I hope so!) solution also exists in Cobol using the same concepts as i=

n
> C or Pascal or java which probably are more teached to students than Co=

bol.
> =


> Regards.
> =


> Alain


Elegance is in the eye of the beholder, to a certain extent. But for
the practical programmer/designer, a sort of "Ockham's Razor" approach
should be applied: if there is more than one solution/method to solve
the problem, then the simplest one is the one to use. As you say, the
other solution is quite easy.

I stand to be corrected on this, of course, but AFAIK recursion (in the
sense of a program or a paragraph calling itself) is rarely necessary.

PL
Michael Wojcik

2006-01-12, 6:55 pm


In article <43C47616.CA7B78FE@mts.net>, Peter Lacey <lacey@mts.net> writes:
>
> I stand to be corrected on this, of course, but AFAIK recursion (in the
> sense of a program or a paragraph calling itself) is rarely necessary.


Recursion is never "necessary", in the strict sense, for any
algorithm that can be implemented in a deterministic Turing machine.
There are proofs by construction that any such recursive algorithm
can be transformed into an iterative one. One, for example, converts
all recursion to tail recursion with continuation-passing; tail
recursion can be replaced by branching, since it's never necessary to
return to previous invocations.

Recursion isn't important because it's necessary. It's important
because it's used extensively in certain branches of mathematics
which provide important results for computer science, and because
it's a way of thinking that's significantly different from iteration -
and thinking also provides important results for computer science :-).

That said, for a trivial function like factorial, the "best"
implementation depends on the goal. Are we implementing it to learn
how to take a common mathematical definition and realize it in a
computer program? Then recursion is better. Are we implementing it
to learn how to use the tools provided by the language in the
simplest and clearest way possible? Then depending on the COBOL
dialect we're targetting, iteration may well be the better choice.
Are we implementing it because we need to make extensive use of it in
calcuations? Then we should be using the built-in function, if there
is one in our dialect; and if not, we should consider what ranges we
need to satisfy, and how we will avoid overflow, and whether it is
critical to performance; and the best choice might not be at all
obvious (though it's unlikely to be recursive).

--
Michael Wojcik michael.wojcik@microfocus.com

This book uses the modern technology to explain the phenomemon in the world
of Japanese animation. If you love anime so much, you'd better read this.
After you read it, you may agree that is destroying the dream of the child.
Needs Chinese viewing system. -- The Goodboy Scientific
Joe Zitzelberger

2006-01-13, 7:55 am

In article <43C3E71D.1476BDC7@mts.net>, Peter Lacey <lacey@mts.net>
wrote:

> Alain Reymond wrote:
> Why teach the more complicated method first? This isn't a case where
> using recursion is preferable. Also (troll-reply) an instructor
> shouldn't get involved in religious wars unless that happens to be the
> course subject.
>
> PL


I would disagree.

For production code, you would write this as a loop. But this is a
great example to help students understand how recursion actually works.

Simple parameters, easy to grasp output....factorial() is made to teach
recursion.

Can you think of a better (e.g. easier to understand) example to
introduce the concept?
Louis Krupp

2006-01-13, 7:55 am

Joe Zitzelberger wrote:
[snip}
> For production code, you would write this as a loop. But this is a
> great example to help students understand how recursion actually works.
>
> Simple parameters, easy to grasp output....factorial() is made to teach
> recursion.
>
> Can you think of a better (e.g. easier to understand) example to
> introduce the concept?


If I had ten minutes to introduce COBOL to people who had never seen it
before, I'd stick with iteration.

Louis
Michael Wojcik

2006-01-13, 9:55 pm


In article <11sf29snp1as55c@corp.supernews.com>, Louis Krupp <lkrupp@pssw.nospam.com.invalid> writes:
> Joe Zitzelberger wrote:
> [snip}

Certainly some of the other classics, such as tree traversal, aren't
an easy fit for traditional COBOL. It's not hard to implement binary
trees in COBOL, with or without pointers, but it's atypical.

Recursive list reversal is pretty clean in COBOL, but the algorithm
is a little more complex than factorial.
[color=darkred]
> If I had ten minutes to introduce COBOL to people who had never seen it
> before, I'd stick with iteration.


Sure, but teaching COBOL using recursion is a rather different task
from teaching recursion using COBOL.

--
Michael Wojcik michael.wojcik@microfocus.com

Unfortunately, as a software professional, tradition requires me to spend New
Years Eve drinking alone, playing video games and sobbing uncontrollably.
-- Peter Johnson
Peter Lacey

2006-01-13, 9:55 pm

Joe Zitzelberger wrote:

<snip>

> For production code, you would write this as a loop. But this is a
> great example to help students understand how recursion actually works.
>
> Simple parameters, easy to grasp output....factorial() is made to teach
> recursion.
>
> Can you think of a better (e.g. easier to understand) example to
> introduce the concept?


To be perfectly frank, I wouldn't mention recursion in a class teaching
Cobol, except in passing as something to be studied as an advanced
topic. In my own (woe is me, long ago) student days, we had topics such
as air-traffic control, programs that could learn, and recursion: all a
waste of time AT THE TIME. I won't dispute that there are a few
students capable of taking such a concept at a tender age and making
much of it but in such a case the instructor's job would be to make
arrangements for the gifted student to surge ahead, not to inflict the
topic on the whole class. Certainly you want to give the group
challenges, to stretch their minds and make them think, but there are
gazillions of more fruitful assignments: the standard file update, both
master and transaction files being sequential, for instance.

To answer your question, I can't think of an easier way to introduce
recursion. But I would be very careful to state that recursion is
probably not the best way to implement it in the real world. Remember
TPB, who'll have to maintain the program!

PL
Howard Brazee

2006-01-13, 9:55 pm

On Fri, 13 Jan 2006 13:08:25 -0600, Peter Lacey <lacey@mts.net> wrote:

>To be perfectly frank, I wouldn't mention recursion in a class teaching
>Cobol, except in passing as something to be studied as an advanced
>topic.


My work has a Thursday afternoon class in JBuilder. Some of the
students already use Java, and skip classes in Java, but most of us
have not used Java professionally.

We won't end up with good Java knowledge - we will end up with the
Java knowledge needed for our work - which will be mainly
communicating between databases and Web services.

So we have a list of subsets that we must know, and another list of
stuff which we can safely ignore for now.

Students often have a hard time narrowing down what they need to
study, and waste time studying the wrong stuff early. Once we learn
the language, I agree most anything is worth learning. But lots of
people never do learn little used features and have long careers.

Getting down how the language works in real life practical examples
makes learning the language easy. Start using the language the way it
was designed to be used, practice it, learn it - and then expand on
its capabilities.
Howard Brazee

2006-01-13, 9:55 pm

On 13 Jan 2006 11:48:16 -0800, "Richard" <riplin@Azonic.co.nz> wrote:

>What is usually done using trees in C is done using ISAM files in
>Cobol, which may in fact be indexed by btrees, or by arrays and SEARCH
>ALL (which may be a binary chop), or with relative files using the
>record numbers as pointers.



Kind of like teaching the difference between sorting an internal table
with a version of CoBOL that doesn't have a command for it, and a
version of CoBOL that does have a command for it.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com