Lazarus

Free Pascal => Beginners => Topic started by: italyrobert on March 12, 2021, 04:05:20 pm

Title: functions in pascal
Post by: italyrobert on March 12, 2021, 04:05:20 pm
So far I know that arraytypes in Pascal is declared with an array length, but is there functions which might cannot be implemented because of that? and maybe still be written in C# for example?
Title: Re: functions in pascal
Post by: marcov on March 12, 2021, 04:08:07 pm
So far I know that arraytypes in Pascal is declared with an array length, but is there functions which might cannot be implemented because of that? and maybe still be written in C# for example?

It is unclear what you mean. Do you mean static array declaration like

Code: Pascal  [Select][+][-]
  1. var
  2.   v : array[2..10] of integer;
  3.  

or dynamic array usage like

Code: Pascal  [Select][+][-]
  1. var v : array of integer;
  2. begin
  3.   setlength(v,10);
  4. end;

Free Pascal as a language has no relation with C# whatsoever, or do you mean with that which one of these are COM compatible?
Title: Re: functions in pascal
Post by: italyrobert on March 12, 2021, 04:37:27 pm
Hi marcov,

I mean the static array

So as you text I can declare an array type as: type somearray = array [2..10] of integer

So I'm looking for an example of a function which cannot be written in pascal because of this type declaration, but on the other hand is possible to write in C# instead.
Title: Re: functions in pascal
Post by: MarkMLl on March 12, 2021, 04:58:20 pm
Hi marcov,

I mean the static array

So as you text I can declare an array type as: type somearray = array [2..10] of integer

So I'm looking for an example of a function which cannot be written in pascal because of this type declaration, but on the other hand is possible to write in C# instead.

In that case I suggest asking in a C# forum, and then coming back here with the response in case it overlooks something.

I for one have better things to do with my time that gratuitously search for claws in my favoured development tool (which doesn't stop me from being critical when I find any :-)

MarkMLl
Title: Re: functions in pascal
Post by: cdbc on March 12, 2021, 05:00:58 pm
Hi
Ok then:
Code: Pascal  [Select][+][-]
  1. var
  2.   a: array[2..10] of byte;
  3.   Idx: integer;
  4. begin
  5.   for Idx:= 2 to 10 do begin
  6.     a[Idx]:= Idx;
  7.     writeln('Array of index: ',a[Idx]);
  8.   end;
  9. end;
  10.  

Sometimes life is easy  ;D
Regards Benny
Title: Re: functions in pascal
Post by: italyrobert on March 12, 2021, 05:19:09 pm
Hi
Ok then:
Code: Pascal  [Select][+][-]
  1. var
  2.   a: array[2..10] of byte;
  3.   Idx: integer;
  4. begin
  5.   for Idx:= 2 to 10 do begin
  6.     a[Idx]:= Idx;
  7.     writeln('Array of index: ',a[Idx]);
  8.   end;
  9. end;
  10.  

Sometimes life is easy  ;D
Regards Benny

Hi cdbc,

But this seems to work as I read it in pascal, because the loop is running in the range of the array 1..10, should this not work? and work instead in c#?
Title: Re: functions in pascal
Post by: marcov on March 12, 2021, 05:34:43 pm
So I'm looking for an example of a function which cannot be written in pascal because of this type declaration, but on the other hand is possible to write in C# instead.

I don't know what C# can do into that detail. But what FPC can't (yet) do is having dynamically (runtime) sized arrays  with a non-zero bound.
Title: Re: functions in pascal
Post by: balazsszekely on March 12, 2021, 05:43:10 pm
What is the question again? A limitation of static arrays in pascal, that does not exists in c#?
Title: Re: functions in pascal
Post by: ASBzone on March 12, 2021, 06:00:34 pm
So far I know that arraytypes in Pascal is declared with an array length...
'


Not always.  Dynamic arrays are supported in Free Pascal.

What are you actually trying to find out?
Title: Re: functions in pascal
Post by: winni on March 12, 2021, 09:07:52 pm
So far I know that arraytypes in Pascal is declared with an array length, but is there functions which might cannot be implemented because of that? and maybe still be written in C# for example?

It is not quiet clear what you mean.

Do you want a correct result or do you want to stop the compiler to moan??

As you are talking about C# I assume the last.

Winni
Title: Re: functions in pascal
Post by: egsuh on March 13, 2021, 04:19:32 am
He is asking something that C# can, but Pascal cannot, because of the definition of static array.  If C# can define something like array with indexes (not values) of [1,4,7,9] then Pascal would not be able to do that.

 
Title: Re: functions in pascal
Post by: cdbc on March 13, 2021, 09:56:38 am
Hi
It's rhe same man, who developed Delphi and C#!!!!
Anders Hejlsberg, with et.al!
A couple of nights reading and "Bob's your uncle"
Sorry, Who's trying here?????????????
Regards Benny
Title: Re: functions in pascal
Post by: italyrobert on March 13, 2021, 10:07:34 am
What is the question again? A limitation of static arrays in pascal, that does not exists in c#?

The static type array declaration in pascal, as I understand make it not possible to implement certain functions and I'm looking for an simple example of a function like that, but it should instead be possible to implement in C# instead,.
Title: Re: functions in pascal
Post by: italyrobert on March 13, 2021, 10:22:27 am
So far I know that arraytypes in Pascal is declared with an array length...
'


Not always.  Dynamic arrays are supported in Free Pascal.

What are you actually trying to find out?

The static type array declaration in pascal, as I understand make it not possible to implement certain functions and I'm looking for an simple example of a function like that, but it should instead be possible to implement in C# instead,.
Title: Re: functions in pascal
Post by: MarkMLl on March 13, 2021, 10:35:55 am
The static type array declaration in pascal, as I understand make it not possible to implement certain functions and I'm looking for an simple example of a function like that, but it should instead be possible to implement in C# instead,.

In that case ask on a C# forum.

As has been pointed out, Turbo Pascal (and later Delphi) and C# had the same designer, and it's reasonable to assume that a high proportion of C# users are intimately familiar with Pascal implementations. However you will need to appreciate that FPC implements a significant amount that's not in "classical" Pascal implementations, and in particular that's not in the ISO definition.

MarkMLl
Title: Re: functions in pascal
Post by: balazsszekely on March 13, 2021, 10:48:06 am
@italyrobert
Quote
The static type array declaration in pascal, as I understand make it not possible to implement certain functions and I'm looking for an simple example of a function like that, but it should instead be possible to implement in C# instead,.
OK, understood. I'm certain that everything you can implement in c#, it can be done in pascal too. You may argue that the c# implementation is more elegant or perhaps easier, but don't forget that c# was developed by Anders Hejlsberg and his team with the knowledge accumulated developing object pascal. On the other hand, some may argue that  c#'s more modern features are overrated.
So what is the c# code that can't be implemented in pascal? I did work with c# in the past, but I cannot recall any magical feature of arrays.

PS: @MarkMLI was faster.
Title: Re: functions in pascal
Post by: italyrobert on March 14, 2021, 10:12:08 am
@italyrobert
Quote
The static type array declaration in pascal, as I understand make it not possible to implement certain functions and I'm looking for an simple example of a function like that, but it should instead be possible to implement in C# instead,.
OK, understood. I'm certain that everything you can implement in c#, it can be done in pascal too. You may argue that the c# implementation is more elegant or perhaps easier, but don't forget that c# was developed by Anders Hejlsberg and his team with the knowledge accumulated developing object pascal. On the other hand, some may argue that  c#'s more modern features are overrated.
So what is the c# code that can't be implemented in pascal? I did work with c# in the past, but I cannot recall any magical feature of arrays.

PS: @MarkMLI was faster.

True, but perhaps the fixet array-size or type of array that could make it impossible for some functions to be implemented in pascal. It is an example of a function with that specific rule that dosnt work I'm looking for, and then it should work in C# instead.
Title: Re: functions in pascal
Post by: WooBean on March 14, 2021, 10:44:35 am
Hi italyrobert,
you had 6 opportunities to ask for assistance to your problem from this forum but you have missed  do it in a proper way.
It seems to me that somebody told/wrote to you that "for sure" c#-language syntacs is more advanced than pascal-language for defininig some functions with using static arrays (as parameters I guess).
If so, you can tell/write that somebody is wrong - pascal syntacs is wider - allows using static arrays (not only 0-based index) and dynamic arrays. In c#, as I remeber, arrays are always 0-based indexed.
See here for start:

https://www.freepascal.org/docs-html/ref/refsu14.html#x38-490003.3.1

WooBean

Title: Re: functions in pascal
Post by: balazsszekely on March 14, 2021, 11:24:34 am
@WooBean
Quote
you had 6 opportunities to ask for assistance to your problem from this forum but you have missed  do it in a proper way.
I don't think he is looking for assistance. More likely he is a c# developer and got into one of those tiresome flame wars about which programming languages is better. Now he must prove to his friend that c#'s array implementation is better then pascal. I'm I right @Robert? :)

The bottom line is each language has pros and cons...Flame wars are useless.
Title: Re: functions in pascal
Post by: cdbc on March 14, 2021, 01:48:00 pm
Hi
Ok here goes:
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/single-dimensional-arrays (https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/single-dimensional-arrays)
Now run along and play nice with the other kids  ;)
Regards Benny
Title: Re: functions in pascal
Post by: marcov on March 14, 2021, 01:57:58 pm
I can't really think of anything, though I only did C# for a relative short time.

The only thing is maybe more seemless boxing from static arrays to dynamic arrays ?

But FPC has open arrays to still allow code to accept variable arrays of both types, so only the mechanism is different.

I think you should go back to your original source that said something couldn't be done in Pascal, and get examples.
Title: Re: functions in pascal
Post by: avk on March 14, 2021, 02:32:38 pm
I recall something like Kernighan's passages on Why Pascal is not my favorite programming language.
Looks like the guy has been stuck somewhere for the last 40 years.
Title: Re: functions in pascal
Post by: MarkMLl on March 14, 2021, 03:21:48 pm
I recall something like Kernighan's passages on Why Pascal is not my favorite programming language.
Looks like the guy has been stuck somewhere for the last 40 years.

Didn't stop him from making a few bucks out of it. However since his book was written in 1976 he was obviously considering a pre-Turbo implementation of the language.

However I would caution that "the language has improved over the last 40 years" is not a fair statement. COBOL and FORTRAN are said by their devotees to be cutting edge because they have had e.g. "object orientation" grafted onto them... an exercise akin to putting lipstick on a pig since we all know what swines the underlying languages were. Pascal OTOH, even as originally defined, has been remarkably influential with almost every successor language at least acknowledging the ideas that it introduced: records, strong typing and so on.

But I still say that this thread's OP is asking his question in the wrong place.

MarkMLl
Title: Re: functions in pascal
Post by: avk on March 14, 2021, 04:10:19 pm
Quote

  ...However since his book was written in 1976..
  It looks like it was in 1981.
Here it is:
Quote
2.1.  The size of an array is part of its type

If one declares
     var     arr10 : array [1..10] of integer;
             arr20 : array [1..20] of integer;
...
...
This botch is the biggest single problem with Pascal...
Curiously, some of his complaints were already irrelevant at the time of publication.
Title: Re: functions in pascal
Post by: MarkMLl on March 14, 2021, 04:34:05 pm
This says it all:

Quote
The size of an array is part of its type

The point there is that an array is a type, and that type is checked. Unlike classic C, where there are primitive types and unchecked arrays.

Now if he'd explicitly said "no provision for open array parameters" or "no provision for extensible arrays" then he would have deserved a bit more sympathy. But complaining that arrays have a type simply indicates that he didn't really understand what he was writing about.

MarkMLl
Title: Re: functions in pascal
Post by: Leledumbo on March 18, 2021, 12:46:05 pm
I don't think he is looking for assistance. More likely he is a c# developer and got into one of those tiresome flame wars about which programming languages is better. Now he must prove to his friend that c#'s array implementation is better then pascal. I'm I right @Robert? :)
I can smell that, too ;)
Sadly, he will only get the other way around as Pascal arrays are a hell lot more flexible than C#. The best that C# can match is declaring arrays on stack using the stackalloc reserved word, but declaring different bounds is only a dream.
Title: Re: functions in pascal
Post by: italyrobert on March 21, 2021, 02:37:56 pm
Quote

  ...However since his book was written in 1976..
  It looks like it was in 1981.
Here it is:
Quote
2.1.  The size of an array is part of its type

If one declares
     var     arr10 : array [1..10] of integer;
             arr20 : array [1..20] of integer;
...
...
This botch is the biggest single problem with Pascal...
Curiously, some of his complaints were already irrelevant at the time of publication.

So with this being the biggest problem, is there any functions, LIKE ANY EXAMPLE, which can't be implemented in pascal because of this. but instead C#?

That's my question. Seems like ther's some misunderstandings going around here
Title: Re: functions in pascal
Post by: avk on March 21, 2021, 03:14:36 pm
It seems I got it right. :)
This "biggest" problem hasn't exists for 40 years. Therefore, I have a different proposal.
When you find the example you want, do a favor and share it with others.
Title: Re: functions in pascal
Post by: MarkMLl on March 21, 2021, 03:24:34 pm
That's my question. Seems like ther's some misunderstandings going around here

No, there are no misunderstandings at all. Find an example which you believe or have told can't be implemented in current Pascal dialects, and post it here for discussion.

It's YOUR responsibility to do that. And it's YOUR privilege, since asking a bunch of people committed to Pascal to point out flaws in it is not likely to get you a useful response.

MarkMLl
Title: Re: functions in pascal
Post by: PascalDragon on March 22, 2021, 09:26:12 am
So with this being the biggest problem, is there any functions, LIKE ANY EXAMPLE, which can't be implemented in pascal because of this. but instead C#?

There is no because of this, because modern Pascal is not restricted to this as everyone else already wrote. In the Pascal of the past (30, 40 years ago) there would be functions that can't be (easily) implemented, but since the era of Delphi (mid 90s) at the latest this is not the case anymore.
Title: Re: functions in pascal
Post by: Rinzwind on March 22, 2021, 11:39:01 am
So far I know that arraytypes in Pascal is declared with an array length, but is there functions which might cannot be implemented because of that? and maybe still be written in C# for example?
No, there are not any functions that cannot be written because of that.
Clear?
Title: Re: functions in pascal
Post by: BeniBela on March 22, 2021, 02:26:08 pm
So with this being the biggest problem, is there any functions, LIKE ANY EXAMPLE, which can't be implemented in pascal because of this. but instead C#?

There is no because of this, because modern Pascal is not restricted to this as everyone else already wrote. In the Pascal of the past (30, 40 years ago) there would be functions that can't be (easily) implemented, but since the era of Delphi (mid 90s) at the latest this is not the case anymore.

You cannot implement writeln(1.0:2:3) for your own types
Title: Re: functions in pascal
Post by: MarkMLl on March 22, 2021, 03:10:14 pm
You cannot implement writeln(1.0:2:3) for your own types

Can you extend the basic syntax in C# to handle things like that? In any event you can do WriteLn(1.0, [2, 3]) in Object Pascal which is /functionally/ equivalent... and OP wasn't asking for exactly the same syntax, just for examples of "things that couldn't be done").

MarkMLl
Title: Re: functions in pascal
Post by: lucamar on March 22, 2021, 03:32:49 pm
In the Pascal of the past (30, 40 years ago) there would be functions that can't be (easily) implemented, but since the era of Delphi (mid 90s) at the latest this is not the case anymore.

This wasn't the case even way before Delphi: most Pascal compilers of the mid-80s (1982 the oldest I know about: Pascal MT+ v5) already allowed for this in one way or other.

The thing is that while valid for what he was talking about, Kerningham's critic was one of the last even remotely founded on fact, and even then most compiler producers had started to solve most of those shortcomings in one way or other.
Title: Re: functions in pascal
Post by: MarkMLl on March 22, 2021, 04:48:43 pm
In the Pascal of the past (30, 40 years ago) there would be functions that can't be (easily) implemented, but since the era of Delphi (mid 90s) at the latest this is not the case anymore.

This wasn't the case even way before Delphi: most Pascal compilers of the mid-80s (1982 the oldest I know about: Pascal MT+ v5) already allowed for this in one way or other.

I never used newer than about v3. Or perhaps v2. And while I might have seen worse compilers it was everything expected from DR: I ended up having to rewrite the runtime library to save x87 state, and it obfuscated its .obj format by appending a rolling zero bit to each byte and packing the result.

Quote
The thing is that while valid for what he was talking about, Kerningham's critic was one of the last even remotely founded on fact, and even then most compiler producers had started to solve most of those shortcomings in one way or other.

The point about Write()-like functions taking multiple parameters and the colon-separated format specification is valid criticism of the early syntax, but the important fact is that there are alternative ways of achieving the same result from Delphi onwards.

However I'd throw in as a detail that Pascal was very much typical of early-70s compilers in that its predefined functions were handled by the compiler (so could have non-standard syntax) rather than there being a more general library mechanism.

Even if Kernighan's criticism was based on fact, it demonstrated that he didn't really /get/ the idea of type checking: a 10-element array is /not/ assignment-compatible with a 9-element array hence it is not compatible in the more general sense of being an interchangeable parameter.

Such things /can/ be a problem if using Pascal or its close relatives for systems programming (e.g. passing network packets around), but Kernighan wasn't claiming to do that so I feel his criticism was out of place.

MarkMLl
Title: Re: functions in pascal
Post by: lucamar on March 22, 2021, 05:16:35 pm
Such things /can/ be a problem if using Pascal or its close relatives for systems programming (e.g. passing network packets around), but Kernighan wasn't claiming to do that so I feel his criticism was out of place.

IMHO, one should read both editions of Software Tools to fully understand what Kerninghan was talking about. He was trying to translate some relatively complex things from Fortran to fairly "standard" Pascal (J&W's) and, logically enough, he found quite some problems.

It matters little nowadays, other than by ignorant people refering to an obsolete criticism to bash on modern Pascal and not remembering that Kerningham himself wrote a partial refutation to that paper later on, after seeing how the language had evolved (even though he said in it that he still preferred C :o)
Title: Re: functions in pascal
Post by: PascalDragon on March 23, 2021, 09:37:58 am
So with this being the biggest problem, is there any functions, LIKE ANY EXAMPLE, which can't be implemented in pascal because of this. but instead C#?

There is no because of this, because modern Pascal is not restricted to this as everyone else already wrote. In the Pascal of the past (30, 40 years ago) there would be functions that can't be (easily) implemented, but since the era of Delphi (mid 90s) at the latest this is not the case anymore.

You cannot implement writeln(1.0:2:3) for your own types

That was not the question however. The question was related to array types and whether you can implement any C# function using such as result types in (modern) Pascal.
Title: Re: functions in pascal
Post by: MarkMLl on March 23, 2021, 09:57:00 am
That was not the question however. The question was related to array types and whether you can implement any C# function using such as result types in (modern) Pascal.

And even if it had been the question there was no requirement that the syntax be identical, only that functionally equivalent code could be written.

MarkMLl
Title: Re: functions in pascal
Post by: ASBzone on March 26, 2021, 05:15:38 am

Did the planet have a lot of tainted water this month?   I've seen more crazy posts this month than I've seen in a long time...
Title: Re: functions in pascal
Post by: dsiders on March 26, 2021, 06:00:00 am

Did the planet have a lot of tainted water this month?   I've seen more crazy posts this month than I've seen in a long time...

Pandemic psychosis.
Title: Re: functions in pascal
Post by: Brizeux on March 26, 2021, 09:49:02 am
A beginner question on functions in FPC pascal : I want to use ArcSin, ArcCos and ArcTan2 of unit math ; is it possible to have access to the ArcTan2.pas source instead of ArcTan2.ppu, to carefully study the implementation of mathematical functions in my code ? because the recipe for, for instance x+iy complex is to use t := 2*ArcTan2( y, x+Sqrt( x*x+y*y ) on the range ]-Pi..Pi] for
the complex logarithm/exponential pseudo-functions. (I shal be glad to incorporate modifications of this function in my code for
mathematical accuracy).  Friendly. Yves .
Title: Re: functions in pascal
Post by: winni on March 26, 2021, 10:16:50 am
A beginner question on functions in FPC pascal : I want to use ArcSin, ArcCos and ArcTan2 of unit math ; is it possible to have access to the ArcTan2.pas source instead of ArcTan2.ppu, to carefully study the implementation of mathematical functions in my code ? because the recipe for, for instance x+iy complex is to use t := 2*ArcTan2( y, x+Sqrt( x*x+y*y ) on the range ]-Pi..Pi] for
the complex logarithm/exponential pseudo-functions. (I shal be glad to incorporate modifications of this function in my code for
mathematical accuracy).  Friendly. Yves .

Hi!

Place your cursor on ArcTan2.
Press Alt + CursorUp.

There you are.

Winni
Title: Re: functions in pascal
Post by: MarkMLl on March 26, 2021, 10:25:30 am
A beginner question on functions in FPC pascal : I want to use ArcSin, ArcCos and ArcTan2 of unit math ; is it possible to have access to the ArcTan2.pas source instead of ArcTan2.ppu, to carefully study the implementation of mathematical functions in my code ? because the recipe for, for instance x+iy complex is to use t := 2*ArcTan2( y, x+Sqrt( x*x+y*y ) on the range ]-Pi..Pi] for
the complex logarithm/exponential pseudo-functions. (I shal be glad to incorporate modifications of this function in my code for
mathematical accuracy).  Friendly. Yves .

Noting what @winni says, this will obviously require that you have the FPC sources installed and assumes that you are using Lazarus as an IDE.

Please note that it is considered very bad form to tack a fresh question onto somebody else's thread. In addition if you want a more detailed answer you should tell us what versions of FPC, Lazarus etc. you're running, and what OS you're using: that will save us having to guess.

MarkMLl
Title: Re: functions in pascal
Post by: Seenkao on March 26, 2021, 11:03:07 am
Не решаемых проблем нет. Есть проблемы, на которые надо потратить больше времени!

Eng: There are no unsolvable problems. There are problems that need to be spent more time!
Title: Re: functions in pascal
Post by: ASBzone on March 31, 2021, 08:32:15 pm

Did the planet have a lot of tainted water this month?   I've seen more crazy posts this month than I've seen in a long time...

Pandemic psychosis.

Good answer! 
TinyPortal © 2005-2018