PascalABC.NET: Difference between revisions

Content deleted Content added
Returned code examples, added a reference
Line 126:
var f: integer -> integer -> integer := x -> y -> x + y;
Writeln(f(2)(6));
</syntaxhighlight>
 
== Code examples ==
PascalABC.NET is a multi-paradigm programming language. It allows one to use different coding styles from oldschool Pascal to functional and object-oriented programming. The same task can be solved in different styles as follows:<ref>{{Cite web |title=PascalABC.NET programming styles |url=https://pascalabcnet.github.io/mydoc_progr_styles.html |access-date=2023-04-09}}</ref>
 
=== Usual PascalABC.NET style ===
<syntaxhighlight lang="delphi">
begin
var (a,b) := ReadInteger2; // read input into tuple of two variables
var sum := 0; // type auto-inference
for var i:=a to b do
sum += i*i;
Print($'Sum = {sum}') // string interpolation
end.
</syntaxhighlight>
 
=== Procedural style ===
<syntaxhighlight lang="delphi">
function SumSquares(a,b: integer): integer;
begin
Result := 0;
for var i := a to b do
Result += i * i
end;
 
begin
var (a,b) := ReadInteger2;
Print($'Sum = {SumSquares(a,b)}')
end.
</syntaxhighlight>
 
=== Functional style ===
This solution uses .NET extension methods for sequences and PascalABC.NET-specific range <code>(a..b)</code>.<syntaxhighlight lang="delphi">
begin
var (a,b) := ReadInteger2;
(a..b).Sum(x -> x*x).Print // method chaining with lambda expressions
end.
</syntaxhighlight>
 
=== Object-oriented style ===
This solution demonstrates PascalABC.NET-specific short function definition style.<syntaxhighlight lang="delphi">
type Algorithms = class
static function SumSquares(a,b: integer) := (a..b).Sum(x -> x*x);
static function SumCubes(a,b: integer) := (a..b).Sum(x -> x*x*x);
end;
 
begin
var (a,b) := ReadInteger2;
Println($'Squares sum = {Algorithms.SumSquares(a,b)}');
Println($'Cubes sum = {Algorithms.SumCubes(a,b)}')
end.
</syntaxhighlight>
 
=== Close to regular C# style ===
It is possible to write programs without usage of PascalABC.NET standard library. All standard .NET Framework classes and methods can be used directly.<syntaxhighlight lang="delphi">
uses System; // using .NET System namespace
begin
var arr := Console.ReadLine.Split(
new char[](' '),
StringSplitOptions.RemoveEmptyEntries
);
var (a,b) := (integer.Parse(arr[0]),integer.Parse(arr[1]));
var sum := 0;
for var i:=a to b do
sum += i*i;
Console.WriteLine($'Sum = {sum}')
end.
</syntaxhighlight>