From: magnus@thep.lu.se (Magnus Olsson)
Subject: Spaghetti in Pascal (was Re: Branching in HLL vs. Assembler (was Re: The need for an assembler))
Organization: Theoretical Physics, Lund University, Sweden
Date: Thu, 3 Sep 1992 11:47:46 GMT

In article <1992Sep3.043632.29125@news.columbia.edu> lasner@watsun.cc.columbia.edu (Charles Lasner) writes:

>Yes, spaghetti code can happen in any language, probably even PASCAL if you
>work at it :-).

At this point, I just can't resist posting the following, which I found
floating around on the local CS VAX several years ago (parts 1 and 3 are
available with anonymous FTP from leif.thep.lu.se, if anyone is mad
enough to be interested):



{   BORED BY PASCAL?    part 2
    ==========================
    In this degenerate age of the toy computer (by some weak souls
    called "personal computer" or something like that) everyone learns
    programming in kindergarten or at least in high school (if there's
    any difference between those school forms). For this activity, a
    language called BASIC is used almost exclusively. BASIC is rather
    like a small subset of FORTRAN with many of its good features
    amputated but with all of its drawbacks. Anyway, you can write
    all kinds of programs in BASIC, and since it (fortunately!) lacks
    any trace of structured constructs it promotes creative programming
    the same way FORTRAN does and you get programmers who THINK while
    they're programming.
    When these persons start a higher education, they are forced
    to write their programs in a "language" called Pascal and are
    thereby quickly converted into well-adapted quiche eaters without
    a trace of originality. This paper was written to point out a method
    of solving this problem.     

    Actually, you don't have to learn Pascal at all - it's much easier
    to write your programs in BASIC and then translate them directly
    into Pascal. As an example, consider the following program. It was
    originally written in BASIC on a ZX80 (Yecch!) and then transformed
    into syntactically perfect Pascal during the pause between two
    lectures. 
    Most Pascal addicts turn away in disgust when seeing the words
    GOTO and LABEL, but there's absolutely no reason to discriminate
    these constructs - after all they're a part of the language (if that's
    not too strong a word to use about Pascal). Why should any programmer
    reject what is good enough for the compiler? Besides, the GOTO was 
    there first - structured programming is a much later (and unnecessary)
    addition. 
    
    By following this recipe, the young, undestroyed programmers can
    continue writing their programs in BASIC, avoid learning more
    Pascal than absolutely necessary and at the same time satisfy the
    Pascal buffs of the university. }
        
PROGRAM NIM (INPUT,OUTPUT);
{(c) Tom A. Crona 1984}
LABEL 70,90,150,230,280,370,400,460,470,520,540,800,810,820;
VAR J,S1,D,E,H,R,K : INTEGER; { WHO NEEDS FANCY LONG VARIABLE NAMES?}
    A,B : ARRAY [0..3] OF INTEGER;
    C   : ARRAY [0..3,0..3] OF INTEGER;

FUNCTION MTH$RANDOM(VAR SEED:INTEGER):REAL; EXTERNAL;

    BEGIN
    WRITELN ('******* NIM *******'); 
    WRITELN ('I HOPE YOU KNOW THE RULES OF NIM.');
    WRITELN ('ANYWAY I''M NOT GOING TO RECITE THEM...'); 
    WRITELN ('ENTER RANDOM SEED');
    READ(S1);	
 70:FOR D:=0 TO 3 DO
    A[D]:=TRUNC(MTH$RANDOM(S1)*15+1);
 90:FOR D:=0 TO 3 DO
    WRITELN (D+1,':',A[D]:2);
    IF A[0]+A[1]+A[2]+A[3]>0 THEN GOTO 150;
    WRITELN ('*I WON*');
    GOTO 540;
150:WRITELN ('WHICH PILE?');
    READ (D);
    IF (D<1) OR (D>4) THEN GOTO 800; 
    WRITELN ('HOW MANY?');
    READ (E);
    IF (E<1) OR (E>A[D-1]) THEN GOTO 810;
    A[D-1]:=A[D-1]-E;
    D:=-1;
230:D:=D+1;
    IF D=4 THEN GOTO 460;
    FOR E:=0 TO 3 DO
    B[E]:=A[E];
280:B[D]:=B[D]-1;
    IF B[D]<0 THEN GOTO 230;
    FOR E:=0 TO 3 DO
    BEGIN
    H:=B[E];
    R:=16;
    FOR J:=0 TO 3 DO
    BEGIN
    C[J,E]:=0;
    R:=R DIV 2;
    IF H<R THEN GOTO 370;
    C[J,E]:=1;
    H:=H-R;
370:END;
    END;
    E:=-1;
400:E:=E+1;
    H:=C[E,0]+C[E,1]+C[E,2]+C[E,3];
    IF H MOD 2>0 THEN GOTO 280;
    IF E<3 THEN GOTO 400;
    A[D]:=B[D];
    GOTO 90;
460:D:=-1;
470:D:=D+1;
    IF D=4 THEN GOTO 520;
    IF A[D]=0 THEN GOTO 470;
    A[D]:=A[D]-TRUNC(MTH$RANDOM(S1)*A[D]+1);
    GOTO 90;
520:WRITELN ('YOU WON.');
540:WRITELN ('ENTER 1 IF YOU WISH TO CONTINUE, 0 OTHERWISE');
    READ (K);
    IF K=1 THEN GOTO 70;
    GOTO 820;
800:WRITELN ('THERE IS NO SUCH PILE, STUPID!!!');
    GOTO 150;
810:WRITELN ('TRYING TO CHEAT, EH?');
    GOTO 150;
820:END.
     

Newsgroups: alt.folklore.computers,alt.religion.computers