Why is this in error?

The place for codemasters or beginners to talk about programming any language for the Spectrum.
User avatar
Zoran
Berk
Posts: 6
Joined: Fri Aug 16, 2019 11:56 am

Re: Why is this in error?

Post by Zoran » Fri Aug 16, 2019 9:13 pm

Joefish wrote:
Fri Aug 16, 2019 6:10 pm
It's easy to overlook the fact that no-one seems to have written a language with - surely - the obvious syntax:

(x, y) = my_function(a, b, c, d)
Python supports this syntax. The following programme:

Code: Select all

def my_function(a, b, c, d):
    return a + b, c + d

x, y = my_function(1, 2, 3, 4)

print(x)
print(y)
will give this output:
3
7
Although such a syntactic sugar is nice, you can also see in this example that you don't have to declare variables explicitly, with their types. Not a language for me. ;)
2 x

llewelyn
Dizzy
Posts: 94
Joined: Thu Feb 22, 2018 3:27 pm
Location: virginias eastern shore
Contact:

Re: Why is this in error?

Post by llewelyn » Fri Aug 16, 2019 10:07 pm

Oh, thats rather nice because for once I could follow it without needing someone to explain it to me! It's a little odd in terms of how the English language would state that but its understandable which is more than can be said for most languages. I've had Python recommended to me by several people including my son who's my guru, he was a Navy radar tech. He just wishes I'd let go of ZXBasic but now after all these years I just couldn't. Its so cute and cuddly!
1 x

AndyC
Manic Miner
Posts: 251
Joined: Mon Nov 13, 2017 5:12 am

Re: Why is this in error?

Post by AndyC » Sat Aug 17, 2019 5:51 am

Joefish wrote:
Fri Aug 16, 2019 6:10 pm
When you get into the habit of forever writing:

x = my_function(a, b)

It's easy to overlook the fact that no-one seems to have written a language with - surely - the obvious syntax:

(x, y) = my_function(a, b, c, d)

Such that a function can return more than one value.

C has lots of ways around this issue (you can define 'x' as a structure (or template) made up of two variables, and the function can return a matching structure; or you can make one of the parameters a pointer to where your variable is stored in memory, so the function can manipulate it directly) but it doesn't have a simple syntax to express it like this.
C# has had exactly that syntax for ages. The return type is a Tuple, but you can automatically deconstruct a Tuple into multiple fields. So if you have a function like:

(int Left, int Right) my_function(int a, int b)
{
return (b,a);
}

Then you can do

int x;
int y;
(x,y) = my_function(3,4)

Or even

var (x,y) = my_function(3,4)

To get the results into two variables x and y, or instead deal with the result as a whole thing, such as:

var t = my_function(3,4)
Console.Writeline($"Left value is {t.Left}, Right value is {t.Right}")

It's a super handy feature, as you can flit between the two representations as and when it makes sense.
0 x

User avatar
djnzx48
Manic Miner
Posts: 541
Joined: Wed Dec 06, 2017 2:13 am
Location: New Zealand

Re: Why is this in error?

Post by djnzx48 » Sat Aug 17, 2019 6:31 am

It's also available as a language feature in C++: https://en.cppreference.com/w/cpp/langu ... ed_binding
0 x

User avatar
1024MAK
Manic Miner
Posts: 514
Joined: Wed Nov 15, 2017 2:52 pm
Location: Sunny Somerset in the U.K. in Europe

Re: Why is this in error?

Post by 1024MAK » Sat Aug 17, 2019 6:34 am

llewelyn wrote:
Fri Aug 16, 2019 10:07 pm
Oh, thats rather nice because for once I could follow it without needing someone to explain it to me! It's a little odd in terms of how the English language would state that but its understandable which is more than can be said for most languages. I've had Python recommended to me by several people including my son who's my guru, he was a Navy radar tech. He just wishes I'd let go of ZXBasic but now after all these years I just couldn't. Its so cute and cuddly!
One important consideration to think about is where technology was when BASIC was formed. Before microprocessors (like the 8080, 6502 and Z80) became available in quantity at relatively low prices, typically BASIC programs were run on mainframe or mini computers that had multiple users connected to them via ‘dumb’ text terminals.
Unless you had your own access, user BASIC programs would be submitted on punched cards, or on programming forms. Then they would be entered as part of a batch job. As an end user, you got a copy of the resulting line printer print out.

Then when BASIC was written for the common microprocessors (like the 8080, 6502 and Z80), because the cost of ROM and RAM was expensive, the size and complexity of the interpreter (language) had to be kept small.

The ZX80 only had a 4Kbyte ROM, the ZX81 had a 8Kbyte ROM and the BASIC ROM in a ZX Spectrum is a 16Kbyte ROM. And even then, the relevant programmer(s) that wrote the code for the BASIC that these machines use, used every trick they could think of to try to cram as much functionality in as they could.

BASIC was also based on even older computer languages.

So now hopefully you can see why there are lots of limitations in BASIC compared to more modern languages. Let alone compared to the (very inexact and vague) English language.

And to this day, we humans still manage to continue to debate what is the best way, and hence there are differences in languages, syntax and symbols even in English text and in mathematics. Just look at how many ways there are of writing the date...

Mark
0 x

llewelyn
Dizzy
Posts: 94
Joined: Thu Feb 22, 2018 3:27 pm
Location: virginias eastern shore
Contact:

Re: Why is this in error?

Post by llewelyn » Thu Aug 22, 2019 1:26 pm

So now hopefully you can see why there are lots of limitations in BASIC compared to more modern languages. Let alone compared to the (very inexact and vague) English language.

And to this day, we humans still manage to continue to debate what is the best way, and hence there are differences in languages, syntax and symbols even in English text and in mathematics. Just look at how many ways there are of writing the date...
Mark

I watched a very interesting lecture by 'Uncle' Bob Martin on structured programming, here's one of his, NOT the one I refer to:-


I mention this because a point that he stressed was the dismal reality of there being so many programming languages whereas ideally, there should be one that covered all the bases. The reason I quoted your text is because in my opinion English (and therefore an ideal form of Basic) is a perfect example of a flexible language which you accuse of being 'inexact and vague'! Its probably the ONLY language in which gender, tense, verb/noun order don't really matter. There's a good reason why this is so because English is a pidgin itself. Which is why its easier to get ideas over to non-native speakers, or more likely vice versa.

I would suggest that the ideal computer language would have similar properties. I've been developing this argument for several years because I truly believe it has validity. Basic for all its sins and imperfections does at least have some of English's properties in that you can get something to work for you in several ways, some really clumsy, some very compact BUT the virtue lies in that flexibility. I'm going to address this in my blog at greater length but I have found some support for the concept which was encouraging.
0 x

User avatar
Einar Saukas
Manic Miner
Posts: 945
Joined: Wed Nov 15, 2017 2:48 pm

Re: Why is this in error?

Post by Einar Saukas » Thu Aug 22, 2019 2:33 pm

Joefish wrote:
Fri Aug 16, 2019 6:10 pm
It's easy to overlook the fact that no-one seems to have written a language with - surely - the obvious syntax:

(x, y) = my_function(a, b, c, d)

Such that a function can return more than one value.
Python, Perl, Lua, Go, ...
0 x

User avatar
Einar Saukas
Manic Miner
Posts: 945
Joined: Wed Nov 15, 2017 2:48 pm

Re: Why is this in error?

Post by Einar Saukas » Thu Aug 22, 2019 2:55 pm

djnzx48 wrote:
Sat Aug 17, 2019 6:31 am
It's also available as a language feature in C++: https://en.cppreference.com/w/cpp/langu ... ed_binding
Yes, but it's not really a natural language feature, more like a hack. It requires some extra "plumbing" to declare such methods in C++.
0 x

User avatar
1024MAK
Manic Miner
Posts: 514
Joined: Wed Nov 15, 2017 2:52 pm
Location: Sunny Somerset in the U.K. in Europe

Re: Why is this in error?

Post by 1024MAK » Thu Aug 22, 2019 2:57 pm

The reason why I describe English as very inexact and vague, is because of it’s flexibility. There are many variations of meanings and words, and expressions just in the U.K., plus various more used around the world.

Humans also love to miss out, or add words to sentences. Or use slang or abbreviations.

And often, you need to have worked out the context of what has been said/written before it’s possible to work out the meaning.

Combined, this makes it rather hard work for a simple logical machine (which is all computers are, they are just really, really fast) to try to work out what a human actually means.

For example, what would a computer make of this expression “let’s see the colour of your money?”

Do you know what it means?

A programming language has to have clear rules and a clear syntax. And must be able to be decoded into logical instruction steps. All decisions ultimately come down to a binary choice (yes/no, on/off etc...).

And there is one big choice facing the people that design a computer language. Do you design it so that it is easier for the computer to translate to its native machine code (that the microprocessor can read directly, so on the ZX Spectrum, that’s Z80 machine code). This is generally called a low level language.

Or do you design it so that it is more human friendly (the requirement for BASIC), but which takes more time for the computer to decode before it can work out what machine code instructions are needed to preform the requested function (hence is part of the reason that BASIC is slow). This is generally called a high level language.

Mark
0 x

Post Reply