Skip to content

Instantly share code, notes, and snippets.

@idkravitz
Last active February 4, 2017 10:23
Show Gist options
  • Select an option

  • Save idkravitz/36601bcd6fc01658fb7f337c281a2388 to your computer and use it in GitHub Desktop.

Select an option

Save idkravitz/36601bcd6fc01658fb7f337c281a2388 to your computer and use it in GitHub Desktop.

Алгол 60

procedure Absmax(a) Size:(n, m) Result:(y) Subscripts:(i, k);
    value n, m; array a; integer n, m, i, k; real y;
comment Наибольший элемент матрицы a, размера n на m 
 передаётся в виде результата в y, а его индексы — в параметры i и k;
begin integer p, q;
    y := 0; i := k := 1;
    for p:=1 step 1 until n do
      for q:=1 step 1 until m do
        if abs(a[p, q]) > y then
        begin y := abs(a[p, q]);
          i := p; k := q
        end
end Absmax

Elliott 803 ALGOL

 FLOATING POINT ALGOL TEST'
 BEGIN REAL A,B,C,D'

 READ D'

 FOR A:= 0.0 STEP D UNTIL 6.3 DO
 BEGIN
   PRINT PUNCH(3),££L??'
   B := SIN(A)'
   C := COS(A)'
   PRINT PUNCH(3),SAMELINE,ALIGNED(1,6),A,B,C'
 END'
 END'

Dartmouth ALGOL 30

BEGIN
FILE F (KIND=REMOTE);
EBCDIC ARRAY E [0:11];
REPLACE E BY "HELLO, WORLD!";
WHILE TRUE DO
  BEGIN
  WRITE (F, *, E);
  END;
END.

ALGOL 68

begin # Algol-68 prime number sieve, functional style #

  proc error = (string s) void:
     (print(( newline, " error: ", s, newline)); goto stop);
  proc one to = (int n) list:
     (proc f = (int m,n) list: (m>n | nil | cons(m, f(m+1,n))); f(1,n));

  mode list = ref node;
  mode node = struct (int h, list t);
  proc cons = (int n, list l) list: heap node := (n,l);
  proc hd   = (list l) int: ( l is nil | error("hd nil"); skip | h of l );
  proc tl   = (list l) list: ( l is nil | error("tl nil"); skip | t of l );
  proc show = (list l) void: ( l isnt nil | print((" ",whole(hd(l),0))); show(tl(l)));

  proc filter = (proc (int) bool p, list l) list:
     if l is nil then nil 
     elif p(hd(l)) then cons(hd(l), filter(p,tl(l)))
     else filter(p, tl(l))
     fi;

  proc sieve = (list l) list:
     if l is nil then nil 
     else
        proc not multiple = (int n) bool: n mod hd(l) ≠ 0;
        cons(hd(l), sieve( filter( not multiple, tl(l) )))
     fi;

  proc primes = (int n) list: sieve( tl( one to(n) ));

  show( primes(100) )
end

COBOL

Снипет 1

      $ SET SOURCEFORMAT"FREE"
IDENTIFICATION DIVISION.
PROGRAM-ID.  MileageCounter.
AUTHOR.  Michael Coughlan.
* Simulates a mileage counter

DATA DIVISION.
WORKING-STORAGE SECTION.
01 Counters.
   02 HundredsCount          PIC 99 VALUE ZEROS.
   02 TensCount              PIC 99 VALUE ZEROS.
   02 UnitsCount             PIC 99 VALUE ZEROS.

01 DisplayItems.
   02 PrnHunds               PIC 9.
   02 PrnTens                PIC 9.
   02 PrnUnits               PIC 9.

PROCEDURE DIVISION.
Begin.
   DISPLAY "Using an out-of-line Perform".
   DISPLAY "About to start mileage counter simulation".
   PERFORM CountMilage
      VARYING HundredsCount FROM 0 BY 1 UNTIL HundredsCount > 9
      AFTER   TensCount FROM 0 BY 1 UNTIL TensCount > 9
      AFTER   UnitsCount FROM 0 BY 1 UNTIL UnitsCount > 9
   DISPLAY "End of mileage counter simulation."

   DISPLAY "Now using in-line Performs"
   DISPLAY "About to start mileage counter simulation".
   PERFORM VARYING HundredsCount FROM 0 BY 1 UNTIL HundredsCount > 9
     PERFORM VARYING TensCount FROM 0 BY 1 UNTIL TensCount > 9
        PERFORM VARYING UnitsCount FROM 0 BY 1 UNTIL UnitsCount > 9
           MOVE HundredsCount TO PrnHunds
           MOVE TensCount     TO  PrnTens
           MOVE UnitsCount    TO PrnUnits
           DISPLAY PrnHunds "-" PrnTens "-" PrnUnits
        END-PERFORM
     END-PERFORM
   END-PERFORM.
   DISPLAY "End of mileage counter simulation."
   STOP RUN.


CountMilage.
   MOVE HundredsCount TO PrnHunds
   MOVE TensCount     TO  PrnTens
   MOVE UnitsCount    TO PrnUnits
   DISPLAY PrnHunds "-" PrnTens "-" PrnUnits.

Снипет 2

      $ SET SOURCEFORMAT"FREE"
IDENTIFICATION DIVISION.
PROGRAM-ID.  Multiplier.
AUTHOR.  Michael Coughlan.
* Example program using ACCEPT, DISPLAY and MULTIPLY to 
* get two single digit numbers from the user and multiply them together

DATA DIVISION.

WORKING-STORAGE SECTION.
01  Num1                                PIC 9  VALUE ZEROS.
01  Num2                                PIC 9  VALUE ZEROS.
01  Result                              PIC 99 VALUE ZEROS.

PROCEDURE DIVISION.
    DISPLAY "Enter first number  (1 digit) : " WITH NO ADVANCING.
    ACCEPT Num1.
    DISPLAY "Enter second number (1 digit) : " WITH NO ADVANCING.
    ACCEPT Num2.
    MULTIPLY Num1 BY Num2 GIVING Result.
    DISPLAY "Result is = ", Result.
    STOP RUN.

Снипет 3

      $ SET SOURCEFORMAT"FREE"
IDENTIFICATION DIVISION.
PROGRAM-ID.  PerformFormat4.
AUTHOR.  Michael Coughlan.
* An example program using the PERFORM..VARYING format.
* Pay particular attention to the values produced by the
* WITH TEST BEFORE and WITH TEST AFTER loops.
* Note that the PERFORM within a PERFORM produces the same
* results as the PERFORM..VARYING..AFTER


DATA DIVISION.
WORKING-STORAGE SECTION.
01  LoopCount          PIC 9  VALUE ZEROS.
01  LoopCount2         PIC S9 VALUE ZEROS.

PROCEDURE DIVISION.
Begin.
    DISPLAY "Start WHILE Iteration of LoopBody"
    PERFORM LoopBody WITH TEST BEFORE
        VARYING LoopCount FROM 1 BY 2
        UNTIL LoopCount GREATER THAN 5.
    DISPLAY "Finished WHILE iteration.  LoopCount = " LoopCount.

    DISPLAY "Start REPEAT Iteration of LoopBody"
    PERFORM LoopBody WITH TEST AFTER
        VARYING LoopCount FROM 1 BY 2 
        UNTIL LoopCount GREATER THAN 5.
    DISPLAY "Finished REPEAT iteration. LoopCount = " LoopCount.

    DISPLAY "Start inline loops"
    PERFORM VARYING LoopCount FROM 1 BY 1 
                    UNTIL LoopCount GREATER THAN 4
       PERFORM VARYING LoopCount2 FROM 5 BY -2 
                       UNTIL LoopCount2 LESS THAN ZERO
          DISPLAY "InLine loop " WITH NO ADVANCING
          DISPLAY "LoopCount = " LoopCount " LoopCount2 = " LoopCount2
       END-PERFORM
    END-PERFORM.
    DISPLAY "Finished inline loops".

    DISPLAY "Start PERFORM VARYING..AFTER".
    PERFORM LoopBody VARYING LoopCount FROM 1 BY 1
                       UNTIL LoopCount GREATER THAN 4
                 AFTER LoopCount2 FROM 5 BY -2
                       UNTIL LoopCount2 LESS THAN ZERO.
    DISPLAY "Finished PERFORM VARYING..AFTER".
    STOP RUN.


LoopBody.
    DISPLAY "LoopBody " WITH NO ADVANCING
    DISPLAY "LoopCount = " LoopCount " LoopCount2 = " LoopCount2.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment