jahboater
Posts: 3044
Joined: Wed Feb 04, 2015 6:38 pm

Re: Making readable modifyable code.

Wed May 30, 2018 10:42 am

Heater wrote:
Wed May 30, 2018 9:45 am
That thing about having to have "return" at the end of a function or not annoys me.
But what about this check at the start:

Code: Select all

if( values == NULL )
   error return .....
1) here there is no meaningful value to put in a "Result" keyword
2) the check has absolutely nothing whatsoever to do with the real purpose of the function - the averaging algorithm.
If I were to write instead:-

Code: Select all

if( values != NULL )
{
   function body
}
Then I have increased the nesting level artificially for the main code - for some logic that is nothing to do with it.
That makes it less readable I think.

The nesting level should reflect the real code logic only.

Yes, yes I know "assert( values != NULL );" would be better, but just to illustrate the point.

Heater
Posts: 10003
Joined: Tue Jul 17, 2012 3:02 pm

Re: Making readable modifyable code.

Wed May 30, 2018 1:02 pm

PeterO,
Now I'm going to have to write this recursive average example in ALGOL-60
You mean like this:

Code: Select all

# Calculate average value of all elements of a vector using a recursive algorithm #
PROC average recursive = ([] REAL vector) REAL:
BEGIN
    # Get length from upper and lower bounds of vector #
    INT length = UPB vector - LWB vector + 1;

    IF length = 0 THEN
        # Cannot have average of zero length vector #
        0.0
    ELIF length = 1 THEN
        # Base case #
        vector[1]
    ELSE
        # Recurse... #
        ((average recursive(vector[2..]) * (length - 1)) + vector[1]) / length
    FI
END;

BEGIN
    # Test data #
    [] REAL test vector  = (100, 200, 100);

    # The test #
    REAL result = average recursive(test vector);
    print (result)
END
Run like so:

Code: Select all

$ a68g  average.a68g
+1.33333333333333e  +2
Isn't that just beautiful? No messy "length" parameter. No messy and dangerous pointers. Or "return" statement. Or wiggly brackets. Oh and that PROC, "average recursive" is a real thing that can be passed around just like an INT.

Oh my God, I'm back in 1976 !

Sorry that is ALGOL 68. ALGOL 60 was a bit before my time :)

jahboater
Posts: 3044
Joined: Wed Feb 04, 2015 6:38 pm

Re: Making readable modifyable code.

Wed May 30, 2018 1:29 pm

Wow impressive - I loved Algol 68 (I used to actually read the revised report!), but I couldn't remember how to program in it now - too long ago.

In C++ perhaps:

average = std::accumulate( x.begin(), x.end(), 0.0 ) / x.size();

works for any arithmetic data type in the array.

Heater
Posts: 10003
Joined: Tue Jul 17, 2012 3:02 pm

Re: Making readable modifyable code.

Wed May 30, 2018 1:45 pm

Took me a little while to remind myself how to do that. Haven't written an Algol program since about 1981.

Hey, using std:: is cheating. It's not even the recursive algorithm.

User avatar
PeterO
Posts: 4257
Joined: Sun Jul 22, 2012 4:14 pm

Re: Making readable modifyable code.

Wed May 30, 2018 1:48 pm

Heater wrote:
Wed May 30, 2018 1:02 pm
PeterO,
Now I'm going to have to write this recursive average example in ALGOL-60
You mean like this:
No quite, as you noted that's Algol-68.... But not had time to do the Algol-60 version yet....

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),Aeromodelling,1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

jahboater
Posts: 3044
Joined: Wed Feb 04, 2015 6:38 pm

Re: Making readable modifyable code.

Wed May 30, 2018 1:56 pm

My first real job was at a university as a systems programmer.
Part of the interview was to write a program, something to do with crystallography in 20 minutes.
I wrote mine in Algol-68, and because of that I was the only one out of 80 applicants to actually complete it.
A number of people chose BASIC and got in an absolutely hopeless mess.

User avatar
PeterO
Posts: 4257
Joined: Sun Jul 22, 2012 4:14 pm

Re: Making readable modifyable code.

Wed May 30, 2018 2:42 pm

Here we go...

Code: Select all

RECURSIVE AVERAGE'
BEGIN REAL ARRAY DATA(0:9)'
INTEGER N'
REAL AV'

REAL PROCEDURE AVERAGE (COUNT,VALUES)' 
VALUE COUNT' 
INTEGER COUNT'
REAL ARRAY VALUES'
BEGIN
REAL AV'
    IF COUNT LESSEQ 0 THEN
       AV := 0.0
    ELSE
       AV := (VALUES(COUNT-1)+(COUNT-1)*AVERAGE(COUNT-1,VALUES))/COUNT' 

    AVERAGE := AV'
END'


FOR N := 0 STEP 1 UNTIL 9 DO DATA(N) := N+1'

AV := AVERAGE(10,DATA)'

PRINT ££L?AV=?,SAMELINE,AV'

END'
And the output

Code: Select all


RECURSIVE AVERAGE
FREE STORE= 4630- 6483



AV= 5.5000000


END OF PROGRAM

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),Aeromodelling,1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

Heater
Posts: 10003
Joined: Tue Jul 17, 2012 3:02 pm

Re: Making readable modifyable code.

Wed May 30, 2018 4:42 pm

PeterO,

Awesome.

What are you running that on?

User avatar
PeterO
Posts: 4257
Joined: Sun Jul 22, 2012 4:14 pm

Re: Making readable modifyable code.

Wed May 30, 2018 4:55 pm

Heater wrote:
Wed May 30, 2018 4:42 pm
PeterO,
Awesome.
What are you running that on?
Hardware:
Today my emulator for the Elliott 803B.
In the future I'll run it on the real Elliott 803B at TNMOC. (https://www.youtube.com/watch?v=AIxZ1i8pvZI 803 compiling and running a programme).

Sofware: The 803's Algol-60 compiler (A104).

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),Aeromodelling,1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

Heater
Posts: 10003
Joined: Tue Jul 17, 2012 3:02 pm

Re: Making readable modifyable code.

Wed May 30, 2018 4:59 pm

Even more awesome.

Can't wait to see it in action.

Return to “General programming discussion”