From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,1d321b3a6b8bcab2 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 1995-02-07 08:58:26 PST Path: nntp.gmd.de!newsserver.jvnc.net!nntpserver.pppl.gov!princeton!udel!gatech!darwin.sura.net!wvnvms!marshall.wvnet.edu!marshall.edu!hathawa2 Newsgroups: comp.lang.ada Subject: Re: "Subtract C, add Ada" Message-ID: <1995Feb7.125826@hobbit> From: hathawa2@marshall.edu (Mark S. Hathaway) Date: 7 Feb 95 12:58:26 EDT References: <3gsr0e$oin@miranda.gmrc.gecm.com> Organization: Marshall University Nntp-Posting-Host: frodo.marshall.edu Date: 1995-02-07T12:58:26-04:00 List-Id: > In article <3gsr0e$oin@miranda.gmrc.gecm.com>, > bill@valiant (R.A.L Williams) writes: > : In article <3gnnfk$bfa@Starbase.NeoSoft.COM> Samuel Mize wrote: > [summary of previous discussions...] > : >...The original > : >C code was: > : > while ( (c = getchar()) == ' ' ) { /* count spaces */ } > : To make truly equivalent Ada code, you would need a function, > : probably in a shared file package, that assigns and returns > : the next character from the file: > : > : package Files_Like_C is > : C: character; > : function Next_Char return character; > : end Files_Like_C; > : > : with Text_Io; use Text_Io; > : package body Files_Like_C is > : function Next_Char return character is > : begin > : if End_Of_File then > : C := Ascii.Nul; -- no EOF character so using NUL > : else -- to emulate the C string terminator > : Get (C); > : return C; > : end if; > : end Next_Char; > : end Files_Like_C; > : > : Once all this infrastructure has been set up, you can code: > : > : while (Next_Char = ' ') loop > : null; -- count spaces here if needed > : end loop; > : > : Before anyone says "but look at all that code!" remember, we > : are recoding the interface to the standard file package, on > : the assumption that there is a compelling reason to put > : Next_Char into expressions. > Never mind all that code, the main feature of the C code was that, at > the termination of the loop, the variable 'c' contained the character > which caused the loop to terminate. Are you suggesting a 'LastChar' > function as well. > > Look, Ada's a great language. Its got lots of innovative features: > built in tasking, compilable package specifications, etc. but, to me, > and, I suspect, to numerous others, the dedication with which 'unsafe' > constructs was eliminated went a little too far, to the detriment > of readability. I feel certain that the C example could be emulated in Ada or many other languages. What is painfully obvious now is that the author of the C code didn't comment it in any way to say precisely what he/she was trying to achieve with that code. If counting spaces was intended, as many of us thought, then the Ada "version" does quite nicely. If the character gotten on the last go around is needed for later then where is the C code which uses that character. Without some followup code it isn't obvious to anyone that that last character will be used in the future, and yet the poster says this was "the main feature of the C code". I can hardly agree. The C version doesn't check for an eof() condition before reading stdin. The author of the Ada code added that check. I'm not certain it's necessry or correct with the object in (standard input stream). Should it ever be at eof()? This is a design decision rather than a programmer's choice. proc count_chars () is var c is character := NUL; count is integer := 0; begin loop c := in.getch(); -- could also be written: c.assign(in.getch()); if c <> ' ' then exit; -- drops out of the loop else count.increment(1); end; end; end count_chars; Anyone can cram it into a smaller form if they like... loop c := in.getch(); if c <> ' ' then exit; else count.increment(1); end; end; but, that's hardly the point. Mark S. Hathaway