g | x | w | all
Bytes Lang Time Link
nan250612T112910ZQOO-OOKA
nan230901T165640ZKai Burg
nan230306T191914Zroblogic
nan230306T185917Zroblogic
nan230306T190819Zroblogic
nan220306T004118Zscpchick
010use string as an array[1‥n] of 0‥ordmaxChar221103T120313ZKai Burg
nan221102T102834ZKai Burg
nan221102T101509ZKai Burg
nan220306T003748Zscpchick
nan211224T065352Ztsh

use case instead of if

101 bytes:

if paramstr(1)='1'then write('hello')else if paramstr(1)='2'then write('golfing')else write('pascal')

82 bytes (-19 bytes)

case paramstr(1)of '1':write('hello');'2':write('golfing')else write('pascal')end;

use index for in membership test

Introduction: All Pascal variants support at least to some extent the in membership operator (in mathematical notation ∊). Extended Pascal, ISO standard 10206, introduces the index function. It requires two char or string arguments (any combination thereof).

index(sample, pattern)

The index function returns the index of the first complete occurrence of pattern in sample. If no such index exists, 0 is returned (an invalid index for any string).

Application: The index function can be utilized to abbreviate set-membership operations. For example

c in['X','Y','Z']       { `c` is a variable possessing the data type `char` }

becomes

index('XYZ',c)>0

Synergies: You can combine this with the string as array[1‥n] of 0‥ord(maxChar) tip. For example

index(')/;CGaek•—§³µãåéïñ', chr(241)) > 0

performs a test whether chr(241) is a Ramanujan prime less than 256. Note, the string literal consists of 23 char values and are not properly displayed on this site.

Dialects: Delphi users want to use pos. The Free Pascal Compiler supports pos, too.

Iterating over the alphabet (or other contiguous ASCII range)

Iterate over the string:

var c:char;begin
for c in'abcdefghijklmnopqrstuvwxyz'do writeln(c);

Or, use an ASCII range (uppercase alphabet)

var j:word;begin
for j:=65to 90do writeln(chr(j));

BUT! Here's the most compact approach, for 50 bytes:

var j:char;begin for j:=#65to#90do writeln(j)end. 

Use the short form of if and for

If your if condition/ for loop only does one statement, omit begin and end.

if <boolean> then begin <statement>;end; 🚫

if <boolean> then <statement>;

for <condition> do begin <statement>;end; 🚫

for <condition> do <statement>;

You can also string them together, e.g.

for <condition> do if <boolean> then <statement>;

Prefer upcase over lowercase

That's 3 whole bytes. Handy for case-insensitive string comparisons.

Variable declaration of same type

var i:word;j:word;
var i,j:word;

Furthermore, all variables can be declared in one var block:

var i,j:word;a,b,c:string;x,y:char;

use string as an array[1‥n] of 0‥ord(maxChar)

In Pascal string literals may contain arbitrary values from the domain of char. Even chr(0) is permitted. The only illegal component is the value denoting a newline (in ASCII chr(10) [“line feed”] and/or chr(13) [“carriage return”]). In other words, multi-line string literals are not allowed. Furthermore the string delimiting character ' (ASCII chr(39)) needs to be escaped (conventionally ''). Example:

program stringArrayDemo(output);
begin
    writeLn(ord(('€Bÿ')[3]))
end.

Since it’s not properly displayed, the above string literal consists of the following four characters (ordinal value in hexadecimal base):

80 42 FF 12

Therefore the above program will print 255 as it’s the third character’s ordinal value. Obviously in your challenge at hand the constant index 3 will be some variable expression and the string literal will have different values.

Since the maxChar value introduced in ISO standard 10206 “Extended Pascal” documents an implementation characteristic, it is imperative that your solution mentions the processor used. The above program will work adequately if compiled with the GNU Pascal Compiler (GPC) or the Free Pascal Compiler (FPC).

prefer repeat … until … loops over while … do begin … end loops

Consideration for guaranteed iteration count ≥ 1: If the loop body of a while loop contains multiple statements, i. e. is surrounded by a proper begin … end, you may consider turning it into a repeat … until loop where the repeat … until by itself constitutes a begin … end frame.

while condition do begin statement;statement end

becomes

repeat statement;statement until not condition

Since a repeat loop’s body is executed at least once, this works fine if it’s guaranteed that the while’s loop body would be executed at least once, too. If condition can legitimately cause zero iterations, this tip does not make sense.

use (already existing) parentheses as token separators

A loop determining the length of input without processing it

while not EOF do begin get(input);i:=i+1 end

can be rearranged into

while not EOF do begin i:=i+1;get(input)end

Similarly in expressions

if ord(c)=32 then

is the same as

if 32=ord(c)then

Since this only saves you one character, it does not make sense to introduce extra parentheses.

Numbers before keywords

for i:=69 to 420 do writeln(i);
for i:=69to 420do writeln(i);

save you 2 byte on for loop

Avoid Integer

VAR var1: Integer; (* Signed, 16 bits in TP7, 32 bits in FPC, It costs 7 bytes. *)
    var2: Word;    (* Unsigned 16 bit, works in most cases. *)
    var3: Int64;   (* Signed 64 bits, Only available in FPC. *)
    var4: 0..9;    (* TP7: same as ShortInt (-128..127), FPC: same as Byte (0..255) *)

Use ^J for line break

WriteLn('abc');Write('def');
Write('abc',chr(10),'def');
Write('abc'#10'def');
Write('abc'^J'def');        (* Both in TP7 and FPC *)

^J is two bytes ^ followed by J, not single byte for Ctrl-J.