less_retarded_wiki/pascal.md
2025-03-26 21:33:03 +01:00

3.6 KiB

Pascal

Pascal (named after French scientist Blaise Pascal) is an old imperative programming language that was commonly used to teach programming and enjoyed wide popularity around 1980s, though it's still used by some to this day. Compared to anything modern, such as Python and JavaScript, Pascal was actually quite good -- it's somewhat similar to C in its paradigm and level of abstraction, and is acceptable as a LRS language. The language was devised by Niklaus Wirth, a Swiss programmer, who implemented it in the year 1970; it was later on standardized by ISO in 1983 (now known as Standard Pascal) and 1990. Pascal also spawned an object oriented dialect called Object Pascal, but that's of course shit that only adds bloat. Likely the best known free software implementations are Free Pascal and GNU Pascal.

Famous part of hacker lore is an essay called Real Programmers Don't Use Pascal that basically goes on a rant about how Pascal is just for pussies and that real men only use assembly and punch cards.

{ Pascal was actually my first language and I have fond memories of it, transitioning to C from Pascal was pretty easy because it had pointers and all this kind of stuff, it was mainly about learning the new syntax. ~drummyfish }

Details

Pascal is an imperative, procedural, structured, relatively lower-level (having pointers etc.), strongly typed, usually compiled, relatively simple language.

Compared to C: Pascal's syntax is different, for example it uses BEGIN and END keywords instead of curly brackets denoting blocks of code; curly brackets are instead used for comments. The syntax is also case-insensitive. Local variables have to always be declared at the start of a function, just like in the old C89. Array indices weirdly start with 1, not 0, which can nowadays be confusing. While in C we have 0 terminated strings, Pascal uses length prefixed strings, i.e. the first value in a string array says the string length. There are also two types of functions: FUNCTIONs (return a value) and PROCEDUREs (don't return anything). Of course the differences don't end here, but these are about the most prominent ones.

Here is our standardized divisor tree program in Pascal:

program divisorTree;
uses crt;

{ recursive function, prints divisor tree of x }
procedure printDivisorTree(x: integer);
var
  a: integer;
  b: integer;
  i: integer;
begin
  a := -1;
  b := -1;

  for i := 2 to x div 2 do { find two closest divisors }
  begin
    if x mod i = 0 then
    begin
      a := i;
      b := x div i;
 
      if b <= a then
        break;
    end;
  end;

  write('(');

  if a > 1 then
  begin
    printDivisorTree(a);
    write(' ',x,' ');
    printDivisorTree(b);
  end
  else
    write(x);

  write(')');
end;

var
  number: integer;
  code: integer;
  userInput: string[16];

begin
  while true do { main loop, read numbers from the user }
  begin
    readLn(userInput);
    val(userInput,number,code);

    if code <> 1 then
    begin
      printDivisorTree(number);
      writeLn('');
    end
    else
      break;
  end;
end.

See Also