Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #76641 > unrolled thread
| Started by | Alla _ <modelling.data@gmail.com> |
|---|---|
| First post | 2015-11-20 08:06 -0800 |
| Last post | 2015-11-20 20:43 -0800 |
| Articles | 20 on this page of 68 — 18 participants |
Back to article view | Back to comp.lang.c
While or if: function to get next operator or operand Alla _ <modelling.data@gmail.com> - 2015-11-20 08:06 -0800
Re: While or if: function to get next operator or operand Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-11-20 08:17 -0800
Re: While or if: function to get next operator or operand Richard Heathfield <rjh@cpax.org.uk> - 2015-11-20 16:44 +0000
Re: While or if: function to get next operator or operand Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-11-20 11:23 -0500
Re: While or if: function to get next operator or operand Alla _ <modelling.data@gmail.com> - 2015-11-20 08:39 -0800
Re: While or if: function to get next operator or operand Richard Heathfield <rjh@cpax.org.uk> - 2015-11-20 16:47 +0000
Re: While or if: function to get next operator or operand Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-11-20 11:54 -0500
Re: While or if: function to get next operator or operand Alla _ <modelling.data@gmail.com> - 2015-11-20 09:13 -0800
Re: While or if: function to get next operator or operand David Thompson <dave.thompson2@verizon.net> - 2015-12-06 08:04 -0500
Re: While or if: function to get next operator or operand James Kuyper <jameskuyper@verizon.net> - 2015-11-20 12:02 -0500
Re: While or if: function to get next operator or operand Richard Heathfield <rjh@cpax.org.uk> - 2015-11-20 16:41 +0000
Re: While or if: function to get next operator or operand Alla _ <modelling.data@gmail.com> - 2015-11-20 09:11 -0800
Re: While or if: function to get next operator or operand Richard Heathfield <rjh@cpax.org.uk> - 2015-11-20 17:39 +0000
Re: While or if: function to get next operator or operand Alla _ <modelling.data@gmail.com> - 2015-11-20 10:42 -0800
Re: While or if: function to get next operator or operand Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2015-11-21 14:58 -0500
Re: While or if: function to get next operator or operand Richard Heathfield <rjh@cpax.org.uk> - 2015-11-23 12:21 +0000
Re: While or if: function to get next operator or operand Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-21 21:29 +0000
Re: While or if: function to get next operator or operand BartC <bc@freeuk.com> - 2015-11-21 22:31 +0000
Re: While or if: function to get next operator or operand BartC <bc@freeuk.com> - 2015-11-22 10:57 +0000
Re: While or if: function to get next operator or operand Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-22 13:45 +0000
Re: While or if: function to get next operator or operand Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2015-11-22 01:31 -0500
Re: While or if: function to get next operator or operand Tim Rentsch <txr@alumni.caltech.edu> - 2015-11-23 04:33 -0800
Re: While or if: function to get next operator or operand Philip Lantz <prl@canterey.us> - 2015-11-23 23:44 -0800
Re: While or if: function to get next operator or operand Rosario19 <Ros@invalid.invalid> - 2015-11-22 09:39 +0100
Re: While or if: function to get next operator or operand Rosario19 <Ros@invalid.invalid> - 2015-11-22 09:49 +0100
Re: While or if: function to get next operator or operand asetofsymbols@gmail.com - 2015-11-22 02:27 -0800
Re: While or if: function to get next operator or operand asetofsymbols@gmail.com - 2015-11-22 02:32 -0800
Re: While or if: function to get next operator or operand Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2015-11-22 10:02 -0500
Re: While or if: function to get next operator or operand Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-23 17:23 +0000
Re: While or if: function to get next operator or operand Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2015-11-23 12:31 -0500
Re: While or if: function to get next operator or operand Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2015-11-23 13:02 -0500
Re: While or if: function to get next operator or operand Alla _ <modelling.data@gmail.com> - 2015-11-22 07:06 -0800
Re: While or if: function to get next operator or operand Barry Schwarz <schwarzb@dqel.com> - 2015-11-22 11:43 -0800
Re: While or if: function to get next operator or operand Alla _ <modelling.data@gmail.com> - 2015-11-24 02:03 -0800
Re: While or if: function to get next operator or operand Alla _ <modelling.data@gmail.com> - 2015-11-24 02:11 -0800
Re: While or if: function to get next operator or operand James Kuyper <jameskuyper@verizon.net> - 2015-11-24 07:09 -0500
Re: While or if: function to get next operator or operand Richard Heathfield <rjh@cpax.org.uk> - 2015-11-23 12:26 +0000
Re: While or if: function to get next operator or operand raltbos@xs4all.nl (Richard Bos) - 2015-11-23 13:26 +0000
Re: While or if: function to get next operator or operand Alla _ <modelling.data@gmail.com> - 2015-11-22 07:17 -0800
Re: While or if: function to get next operator or operand James Kuyper <jameskuyper@verizon.net> - 2015-11-20 12:15 -0500
Re: While or if: function to get next operator or operand Alla _ <modelling.data@gmail.com> - 2015-11-20 10:44 -0800
Re: While or if: function to get next operator or operand Tim Rentsch <txr@alumni.caltech.edu> - 2015-11-20 12:31 -0800
Re: While or if: function to get next operator or operand Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2015-11-20 15:38 -0500
Re: While or if: function to get next operator or operand James Kuyper <jameskuyper@verizon.net> - 2015-11-20 16:14 -0500
Re: While or if: function to get next operator or operand Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2015-11-20 16:55 -0500
Re: While or if: function to get next operator or operand Geoff <geoff@invalid.invalid> - 2015-11-20 15:04 -0800
Re: While or if: function to get next operator or operand Barry Schwarz <schwarzb@dqel.com> - 2015-11-20 20:59 -0800
Re: While or if: function to get next operator or operand Alla _ <modelling.data@gmail.com> - 2015-11-20 23:50 -0800
Re: While or if: function to get next operator or operand Alla _ <modelling.data@gmail.com> - 2015-11-21 00:18 -0800
Re: While or if: function to get next operator or operand Tim Rentsch <txr@alumni.caltech.edu> - 2015-11-22 14:10 -0800
Re: While or if: function to get next operator or operand Philip Lantz <prl@canterey.us> - 2015-11-22 19:00 -0800
Re: While or if: function to get next operator or operand James Kuyper <jameskuyper@verizon.net> - 2015-11-23 06:57 -0500
Re: While or if: function to get next operator or operand Philip Lantz <prl@canterey.us> - 2015-11-23 23:49 -0800
Re: While or if: function to get next operator or operand James Kuyper <jameskuyper@verizon.net> - 2015-11-24 07:13 -0500
Re: While or if: function to get next operator or operand Tim Rentsch <txr@alumni.caltech.edu> - 2015-11-23 07:55 -0800
Re: While or if: function to get next operator or operand Philip Lantz <prl@canterey.us> - 2015-11-23 23:36 -0800
Re: While or if: function to get next operator or operand Tim Rentsch <txr@alumni.caltech.edu> - 2015-11-27 13:32 -0800
Re: While or if: function to get next operator or operand Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-11-20 16:13 -0500
Re: While or if: function to get next operator or operand Tim Rentsch <txr@alumni.caltech.edu> - 2015-11-22 13:54 -0800
Re: While or if: function to get next operator or operand Geoff <geoff@invalid.invalid> - 2015-11-20 12:19 -0800
Re: While or if: function to get next operator or operand Tim Rentsch <txr@alumni.caltech.edu> - 2015-11-20 15:32 -0800
Re: While or if: function to get next operator or operand Chad <cdalten@gmail.com> - 2015-11-20 18:38 -0800
Re: While or if: function to get next operator or operand Keith Thompson <kst-u@mib.org> - 2015-11-20 19:45 -0800
Re: While or if: function to get next operator or operand Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-11-20 20:12 -0800
Re: While or if: function to get next operator or operand Alla _ <modelling.data@gmail.com> - 2015-11-20 20:45 -0800
Re: While or if: function to get next operator or operand Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-11-20 21:04 -0800
Re: While or if: function to get next operator or operand Tim Rentsch <txr@alumni.caltech.edu> - 2015-11-22 13:57 -0800
Re: While or if: function to get next operator or operand Alla _ <modelling.data@gmail.com> - 2015-11-20 20:43 -0800
Page 1 of 4 [1] 2 3 4 Next page →
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-11-20 08:06 -0800 |
| Subject | While or if: function to get next operator or operand |
| Message-ID | <ca6f25b9-c067-4945-b4dd-2ba0a97ec136@googlegroups.com> |
Hello!
Please, take a look at the function below. It is from K&R.
I dare to wonder why they use a while loop if the condition
considers only one item in the list. Why not to use if statement?
What is the purpose and the logic behind the while loop in this case?
I have accidentally found confirmation of my
concern on stackoverflow:
QUOTE:
Lines like while ((s[0] = c = getch()) == ' ' || c == '\t'); make me wanna weep. - ammoQ
@ammoQ Indeed. And then people still recommend K&R, I have absolutely no idea why. Look at this horrible code, it is not written by the OP, it is written by K&R! Had any rookie programmer written code like this today, he would have been immediately fired. - Lundin
UNQUOTE
Function:
/* getop: get next character or numeric operand */
int getop(char s[])
{
int i, c;
/* the below while loop */
while ((s[0] = c = getch()) == ' ' || c == '\t')
;
s[1] = '\0';
if (!isdigit(c) && c != '.')
return c; /* not a number */
i = 0;
if (isdigit(c)) /* collect integer part */
while (isdigit(s[++i] = c = getch()))
;
if (c == '.') /* collect fraction part */
while (isdigit(s[++i] = c = getch()))
;
s[i] = '\0';
if (c != EOF)
ungetch(c);
return NUMBER;
}
Thank you!
[toc] | [next] | [standalone]
| From | Malcolm McLean <malcolm.mclean5@btinternet.com> |
|---|---|
| Date | 2015-11-20 08:17 -0800 |
| Message-ID | <bbdd9327-34e1-417f-98c8-c33bb3092eaf@googlegroups.com> |
| In reply to | #76641 |
On Friday, November 20, 2015 at 4:07:35 PM UTC, Alla _ wrote:
> Hello!
>
> Please, take a look at the function below. It is from K&R.
>
> I dare to wonder why they use a while loop if the condition
> considers only one item in the list. Why not to use if statement?
> What is the purpose and the logic behind the while loop in this case?
>
> I have accidentally found confirmation of my
> concern on stackoverflow:
>
> QUOTE:
> Lines like while ((s[0] = c = getch()) == ' ' || c == '\t'); make me wanna weep. - ammoQ
>
K and R's achievement was remarkable, but they're not totally infallible.
C written like that is a bad idea. Every condition should be simple and understandable.
What the loop is doing is putting a non-whitespace character in s[0],
The way to write the loop is
int ch;
while( (ch = getch()) != EOF)
{
if(ch == ' ' || ch == '\t')
continue;
else
{
s[0] = ch;
break;
}
}
ten lines rather than one, but ten simple lines, making it abundantly clear what the intention
of the code is.
[toc] | [prev] | [next] | [standalone]
| From | Richard Heathfield <rjh@cpax.org.uk> |
|---|---|
| Date | 2015-11-20 16:44 +0000 |
| Message-ID | <n2nigl$n2h$2@dont-email.me> |
| In reply to | #76642 |
On 20/11/15 16:17, Malcolm McLean wrote:
> On Friday, November 20, 2015 at 4:07:35 PM UTC, Alla _ wrote:
>> Hello!
>>
>> Please, take a look at the function below. It is from K&R.
>>
>> I dare to wonder why they use a while loop if the condition
>> considers only one item in the list. Why not to use if statement?
>> What is the purpose and the logic behind the while loop in this case?
>>
>> I have accidentally found confirmation of my
>> concern on stackoverflow:
>>
>> QUOTE:
>> Lines like while ((s[0] = c = getch()) == ' ' || c == '\t'); make me wanna weep. - ammoQ
>>
> K and R's achievement was remarkable, but they're not totally infallible.
Agreed.
> C written like that is a bad idea.
What's wrong with it?
> Every condition should be simple and understandable.
It is!
> What the loop is doing is putting a non-whitespace character in s[0],
>
> The way to write the loop is
>
> int ch;
>
> while( (ch = getch()) != EOF)
> {
> if(ch == ' ' || ch == '\t')
> continue;
> else
> {
> s[0] = ch;
> break;
I'd fail you right there. But then I'm quite a tough crowd when it comes
to program structure.
--
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Date | 2015-11-20 11:23 -0500 |
| Message-ID | <n2nhag$hv4$1@dont-email.me> |
| In reply to | #76641 |
On 11/20/2015 11:06 AM, Alla _ wrote:
> Hello!
>
> Please, take a look at the function below. It is from K&R.
>
> I dare to wonder why they use a while loop if the condition
> considers only one item in the list. Why not to use if statement?
> What is the purpose and the logic behind the while loop in this case?
>[...]
> /* the below while loop */
> while ((s[0] = c = getch()) == ' ' || c == '\t')
> ;
It's not looking at "only one item," it's looking at as many
leading space or tab characters as getch() supplies. The loop
terminates when the supplied character is something other than a
space or tab, and that non-space/tab character is stored in s[0].
--
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM
[toc] | [prev] | [next] | [standalone]
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-11-20 08:39 -0800 |
| Message-ID | <beead803-e6f9-487d-a3da-d2777d5ce30a@googlegroups.com> |
| In reply to | #76643 |
On Friday, November 20, 2015 at 8:24:14 PM UTC+4, Eric Sosman wrote: > On 11/20/2015 11:06 AM, Alla _ wrote: > > Hello! > > > > Please, take a look at the function below. It is from K&R. > > > > I dare to wonder why they use a while loop if the condition > > considers only one item in the list. Why not to use if statement? > > What is the purpose and the logic behind the while loop in this case? > >[...] > > /* the below while loop */ > > while ((s[0] = c = getch()) == ' ' || c == '\t') > > ; > > It's not looking at "only one item," it's looking at as many > leading space or tab characters as getch() supplies. The loop > terminates when the supplied character is something other than a > space or tab, and that non-space/tab character is stored in s[0]. > No. It looks if the input is equal to a space or a tab, and if yes, it assigns a space or a tab to s[0]. After that, s[1] = '\0', and the array is complete.
[toc] | [prev] | [next] | [standalone]
| From | Richard Heathfield <rjh@cpax.org.uk> |
|---|---|
| Date | 2015-11-20 16:47 +0000 |
| Message-ID | <n2nim6$oci$1@dont-email.me> |
| In reply to | #76644 |
On 20/11/15 16:39, Alla _ wrote: > On Friday, November 20, 2015 at 8:24:14 PM UTC+4, Eric Sosman wrote: >> On 11/20/2015 11:06 AM, Alla _ wrote: >>> Hello! >>> >>> Please, take a look at the function below. It is from K&R. >>> >>> I dare to wonder why they use a while loop if the condition >>> considers only one item in the list. Why not to use if statement? >>> What is the purpose and the logic behind the while loop in this case? >>> [...] >>> /* the below while loop */ >>> while ((s[0] = c = getch()) == ' ' || c == '\t') >>> ; >> >> It's not looking at "only one item," it's looking at as many >> leading space or tab characters as getch() supplies. The loop >> terminates when the supplied character is something other than a >> space or tab, and that non-space/tab character is stored in s[0]. >> > No. Actually, yes. Eric Sosman is not infallible, but you'll have to work a lot harder than that to catch him out. He is quite right here (in fact, he is almost always quite right, and sometimes he's very, very right), and you are quite wrong to say otherwise on this occasion. > It looks if the input is equal to a space or a tab, and if yes, it > assigns a space or a tab to s[0]. And if *not*, it keeps looking. That's why it's a loop, not an if. -- Richard Heathfield Email: rjh at cpax dot org dot uk "Usenet is a strange place" - dmr 29 July 1999 Sig line 4 vacant - apply within
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Date | 2015-11-20 11:54 -0500 |
| Message-ID | <n2nj3c$q82$1@dont-email.me> |
| In reply to | #76644 |
On 11/20/2015 11:39 AM, Alla _ wrote:
> On Friday, November 20, 2015 at 8:24:14 PM UTC+4, Eric Sosman wrote:
>> On 11/20/2015 11:06 AM, Alla _ wrote:
>>> Hello!
>>>
>>> Please, take a look at the function below. It is from K&R.
>>>
>>> I dare to wonder why they use a while loop if the condition
>>> considers only one item in the list. Why not to use if statement?
>>> What is the purpose and the logic behind the while loop in this case?
>>> [...]
>>> /* the below while loop */
>>> while ((s[0] = c = getch()) == ' ' || c == '\t')
>>> ;
>>
>> It's not looking at "only one item," it's looking at as many
>> leading space or tab characters as getch() supplies. The loop
>> terminates when the supplied character is something other than a
>> space or tab, and that non-space/tab character is stored in s[0].
>>
> No. It looks if the input is equal to a space or a tab, and if yes, it
> assigns a space or a tab to s[0]. After that, s[1] = '\0', and the
> array is complete.
No. The loop stores *every* character from getch() into c and
into s[0]. Then if the character is a space or tab, the loop does
it all over again, calling getch() and storing its result in c and
in s[0], overwriting the space or tab from the first iteration. And
if the second character is a space or tab the loop repeats yet again
for a third, fourth, ... character, storing each of them in c and s[0],
again and again.
Finally (we expect), getch() produces a character that is not a
space or tab. Like all the others, this character is stored in c
and in s[0], but then the test fails: The character is neither a
space nor a tab, so the loop stops and execution moves onward to
the next statement.
What is the situation at this point? s[0] and c hold the same
value, which is the last one so far returned by getch(), and that
character is known to be something other than a space or tab. There
is nothing (in the fragment shown) that touches or uses s[1] at all.
--
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM
[toc] | [prev] | [next] | [standalone]
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-11-20 09:13 -0800 |
| Message-ID | <7591945d-b27b-4331-a243-d892d95ab593@googlegroups.com> |
| In reply to | #76649 |
On Friday, November 20, 2015 at 8:54:26 PM UTC+4, Eric Sosman wrote: > On 11/20/2015 11:39 AM, Alla _ wrote: > > On Friday, November 20, 2015 at 8:24:14 PM UTC+4, Eric Sosman wrote: > >> On 11/20/2015 11:06 AM, Alla _ wrote: > >>> Hello! > >>> > >>> Please, take a look at the function below. It is from K&R. > >>> > >>> I dare to wonder why they use a while loop if the condition > >>> considers only one item in the list. Why not to use if statement? > >>> What is the purpose and the logic behind the while loop in this case? > >>> [...] > >>> /* the below while loop */ > >>> while ((s[0] = c = getch()) == ' ' || c == '\t') > >>> ; > >> > >> It's not looking at "only one item," it's looking at as many > >> leading space or tab characters as getch() supplies. The loop > >> terminates when the supplied character is something other than a > >> space or tab, and that non-space/tab character is stored in s[0]. > >> > > No. It looks if the input is equal to a space or a tab, and if yes, it > > assigns a space or a tab to s[0]. After that, s[1] = '\0', and the > > array is complete. > > No. The loop stores *every* character from getch() into c and > into s[0]. Then if the character is a space or tab, the loop does > it all over again, calling getch() and storing its result in c and > in s[0], overwriting the space or tab from the first iteration. And > if the second character is a space or tab the loop repeats yet again > for a third, fourth, ... character, storing each of them in c and s[0], > again and again. > > Finally (we expect), getch() produces a character that is not a > space or tab. Like all the others, this character is stored in c > and in s[0], but then the test fails: The character is neither a > space nor a tab, so the loop stops and execution moves onward to > the next statement. > > What is the situation at this point? s[0] and c hold the same > value, which is the last one so far returned by getch(), and that > character is known to be something other than a space or tab. There > is nothing (in the fragment shown) that touches or uses s[1] at all. > Ah! I see it now. I truly didn't see this. I am sorry.
[toc] | [prev] | [next] | [standalone]
| From | David Thompson <dave.thompson2@verizon.net> |
|---|---|
| Date | 2015-12-06 08:04 -0500 |
| Message-ID | <4ic86btfso61lfp7idn96u8mgauggtiuse@4ax.com> |
| In reply to | #76649 |
On Fri, 20 Nov 2015 11:54:11 -0500, Eric Sosman <esosman@comcast-dot-net.invalid> wrote: > On 11/20/2015 11:39 AM, Alla _ wrote: <snips> > >>> while ((s[0] = c = getch()) == ' ' || c == '\t') > >>> ; <snip incorrect description> > No. The loop stores *every* character from getch() into c and > into s[0]. Then if the character is a space or tab, the loop does > it all over again, <snip> > Finally (we expect), getch() produces a character that is not a > space or tab. <snip> Yes. > What is the situation at this point? s[0] and c hold the same > value, which is the last one so far returned by getch(), and that > character is known to be something other than a space or tab. There > is nothing (in the fragment shown) that touches or uses s[1] at all. The original post does set s[1]='\0'; immediately after the loop, but that got snipped upthread.
[toc] | [prev] | [next] | [standalone]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Date | 2015-11-20 12:02 -0500 |
| Message-ID | <564F5220.6090604@verizon.net> |
| In reply to | #76644 |
On 11/20/2015 11:39 AM, Alla _ wrote: > On Friday, November 20, 2015 at 8:24:14 PM UTC+4, Eric Sosman wrote: >> On 11/20/2015 11:06 AM, Alla _ wrote: >>> Hello! >>> >>> Please, take a look at the function below. It is from K&R. >>> >>> I dare to wonder why they use a while loop if the condition >>> considers only one item in the list. Why not to use if statement? >>> What is the purpose and the logic behind the while loop in this case? >>> [...] >>> /* the below while loop */ >>> while ((s[0] = c = getch()) == ' ' || c == '\t') >>> ; >> >> It's not looking at "only one item," it's looking at as many >> leading space or tab characters as getch() supplies. The loop >> terminates when the supplied character is something other than a >> space or tab, and that non-space/tab character is stored in s[0]. >> > No. It looks if the input is equal to a space or a tab, and if yes, it > assigns a space or a tab to s[0]. ... Not quite. First of all, it always assigns the value of c to s[0], regardless of the value of c. It then checks whether that value is ' ' or '\t'. The key question you should ask yourself is what is the value the controlling expression if c==' '? Also, what is the value of the controlling expression if c=='\t'? What is the value of the controlling expression if c isn't equal to either ' ' or '\t'? Are any of those three values non-zero? The answer is "yes" - if you disagree, you've made a mistake. Please explain why you think the value is always zero, and I'll try to explain what your mistake is. The loop will continue executing as long as the value of the controlling expression is non-zero. > ... After that, s[1] = '\0', and the > array is complete. That doesn't happen until the loop exits. In other words, it doesn't happen until the controlling expression has a value of zero.
[toc] | [prev] | [next] | [standalone]
| From | Richard Heathfield <rjh@cpax.org.uk> |
|---|---|
| Date | 2015-11-20 16:41 +0000 |
| Message-ID | <n2nibm$n2h$1@dont-email.me> |
| In reply to | #76641 |
On 20/11/15 16:06, Alla _ wrote: > Hello! > > Please, take a look at the function below. It is from K&R. It's from page 78 of K&R. Yesterday you were on page 29. Do you think it's some kind of race? > I dare to wonder why they use a while loop if the condition > considers only one item in the list. Why not to use if statement? If you had paid closer attention to the previous pages, and to the actual code, you would see what's going on. > What is the purpose and the logic behind the while loop in this case? I think you could ask a better question, such as, "Why, with a track record of misunderstanding really simple stuff, have I skipped over 50 pages of extraordinarily dense material in a single day?" > I have accidentally found confirmation of my > concern on stackoverflow: > > QUOTE: > Lines like while ((s[0] = c = getch()) == ' ' || c == '\t'); make me wanna weep. - ammoQ > > @ammoQ Indeed. And then people still recommend K&R, I have absolutely no idea why. Look at this horrible code, it is not written by the OP, it is written by K&R! Had any rookie programmer written code like this today, he would have been immediately fired. - Lundin > > UNQUOTE The line is obvious and the idiom obvious. People recommend K&R because it's a stunningly good book written by stunningly good programmers. As for ammoQ and Lundin, I might be more willing to pay attention to their views on language once they have mastered English. Alla, I have tried very, very hard to help you over the last few weeks, but you're making it difficult. Your current question shows that you are still not *reading* the code. The purpose of the while loop is blindingly obvious to me. That doesn't mean it will necessarily be blindingly obvious to you - I do realise that - but if you sit down with a pen and some paper and step through it *exactly*, you'll soon see what's going on. And what happened to the last 50-odd pages of K&R (30-77)? Did you just skim-read them? I think you need to start again at Chapter 1, and this time proceed at about 1-2% of your previous speed. You would also do well to form your own opinion of C after you've learned it, rather than taking your cue from the functionally illiterate. -- Richard Heathfield Email: rjh at cpax dot org dot uk "Usenet is a strange place" - dmr 29 July 1999 Sig line 4 vacant - apply within
[toc] | [prev] | [next] | [standalone]
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-11-20 09:11 -0800 |
| Message-ID | <8b49cc13-fc55-470d-8941-02ba737e3257@googlegroups.com> |
| In reply to | #76645 |
On Friday, November 20, 2015 at 8:41:47 PM UTC+4, Richard Heathfield wrote: > On 20/11/15 16:06, Alla _ wrote: > > Hello! > > > > Please, take a look at the function below. It is from K&R. > > It's from page 78 of K&R. Yesterday you were on page 29. Do you think > it's some kind of race? > > > I dare to wonder why they use a while loop if the condition > > considers only one item in the list. Why not to use if statement? > > If you had paid closer attention to the previous pages, and to the > actual code, you would see what's going on. > > > What is the purpose and the logic behind the while loop in this case? > > I think you could ask a better question, such as, "Why, with a track > record of misunderstanding really simple stuff, have I skipped over 50 > pages of extraordinarily dense material in a single day?" > > > I have accidentally found confirmation of my > > concern on stackoverflow: > > > > QUOTE: > > Lines like while ((s[0] = c = getch()) == ' ' || c == '\t'); make me wanna weep. - ammoQ > > > > @ammoQ Indeed. And then people still recommend K&R, I have absolutely no idea why. Look at this horrible code, it is not written by the OP, it is written by K&R! Had any rookie programmer written code like this today, he would have been immediately fired. - Lundin > > > > UNQUOTE > > The line is obvious and the idiom obvious. People recommend K&R because > it's a stunningly good book written by stunningly good programmers. As > for ammoQ and Lundin, I might be more willing to pay attention to their > views on language once they have mastered English. > > Alla, I have tried very, very hard to help you over the last few weeks, > but you're making it difficult. Your current question shows that you are > still not *reading* the code. The purpose of the while loop is > blindingly obvious to me. That doesn't mean it will necessarily be > blindingly obvious to you - I do realise that - but if you sit down with > a pen and some paper and step through it *exactly*, you'll soon see > what's going on. > > And what happened to the last 50-odd pages of K&R (30-77)? Did you just > skim-read them? I think you need to start again at Chapter 1, and this > time proceed at about 1-2% of your previous speed. > > You would also do well to form your own opinion of C after you've > learned it, rather than taking your cue from the functionally illiterate. > > -- > Richard Heathfield > Email: rjh at cpax dot org dot uk > "Usenet is a strange place" - dmr 29 July 1999 > Sig line 4 vacant - apply within I am not daring to make any opinion of C, or of writers of this or that book. But I am showing my misunderstanding and confusion, which is based (though unjustly) on what I have learned thus far, which is a tiny fraction of a percent of what I could have achieved in other circumstances. Yes, I am aware that K$R book is the best book on C, and it's said everywhere. I am posting questions here only if I am sure I don't see the answer, even after writing down the process, googling my questions, etc. ) I have not skipped through pages. The only thing I have been doing during the last three days is reading and studying K&R.
[toc] | [prev] | [next] | [standalone]
| From | Richard Heathfield <rjh@cpax.org.uk> |
|---|---|
| Date | 2015-11-20 17:39 +0000 |
| Message-ID | <n2nloh$6gu$1@dont-email.me> |
| In reply to | #76651 |
On 20/11/15 17:11, Alla _ wrote: <snip> > I have not skipped through pages. The only thing I have been doing > during the last three days is reading and studying K&R. I was a touch harsh, for which I apologise. But you are still going way too fast. You are trying to absorb new material without having a firm foundation in what has gone before. To demonstrate this, I would like to offer you a little challenge if I may. (No doubt other people will also chip in their own answers, but I would urge you to ignore those answers until you have come up with your own solution.) Here's the challenge: Write a correct C program that reads standard input and reproduces it on standard output verbatim *except that* each word (which I define as a contiguous series of letters terminated on each end by a non-letter or by one end or other of the input) is displayed with the inner letters (i.e. not the first and last letters) sorted into alphabetical order. The first and last letters should stay in their correct positions. I will now demonstrate what I mean, using this paragraph as input. Wirte a cceorrt C pagorrm taht rades saadnrtd input and rcdeeoprus it on saadnrtd optuut vabeirtm *ecepxt taht* each word (wchih I define as a cginootuus seeirs of leettrs taeeimnrtd on each end by a non-leettr or by one end or oehtr of the input) is daeilpsyd with the iennr leettrs (i.e. not the first and last leertts) seortd into aaabcehilptl oderr. The first and last leertts shloud saty in tehir cceorrt piinoosts. I will now daemnorstte waht I maen, uinsg this paaagprrh as input. You may assume that no word exceeds 64 letters in length. If you have absorbed everything you have read in K&R up to page 77, this will be a very easy exercise, and you will not need any help with it. So if you struggle with it (and I think you will), that is a sign that you are going too fast. -- Richard Heathfield Email: rjh at cpax dot org dot uk "Usenet is a strange place" - dmr 29 July 1999 Sig line 4 vacant - apply within
[toc] | [prev] | [next] | [standalone]
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-11-20 10:42 -0800 |
| Message-ID | <22bad07f-912a-4729-aa3a-c9ae3d637f99@googlegroups.com> |
| In reply to | #76655 |
> To demonstrate this, I would like to offer you a little challenge if I > may. (No doubt other people will also chip in their own answers, but I > would urge you to ignore those answers until you have come up with your > own solution.) > > If you have absorbed everything you have read in K&R up to page 77, this > will be a very easy exercise, and you will not need any help with it. > > So if you struggle with it (and I think you will), that is a sign that > you are going too fast. > > I love this. Thank you very much. I will come back with the solution, or questions; the later is more likely, as you correctly stated. Even if solutions appear before I post my version, I won't look at them.
[toc] | [prev] | [next] | [standalone]
| From | Lew Pitcher <lew.pitcher@digitalfreehold.ca> |
|---|---|
| Date | 2015-11-21 14:58 -0500 |
| Message-ID | <D144y.107420$KK.41499@fx29.iad> |
| In reply to | #76655 |
Just a note on testing code for Richard's challenge.
On Friday November 20 2015 12:39, in comp.lang.c, "Richard Heathfield"
<rjh@cpax.org.uk> wrote:
[snip]
> Here's the challenge:
>
> Write a correct C program that reads standard input and reproduces it on
> standard output verbatim *except that* each word (which I define as a
> contiguous series of letters terminated on each end by a non-letter or
> by one end or other of the input) is displayed with the inner letters
> (i.e. not the first and last letters) sorted into alphabetical order.
> The first and last letters should stay in their correct positions. I
> will now demonstrate what I mean, using this paragraph as input.
For those who have taken up Richard's challenge, a note
> Wirte a cceorrt C pagorrm taht rades saadnrtd input and rcdeeoprus it on
> saadnrtd optuut vabeirtm *ecepxt taht* each word (wchih I define as a
> cginootuus seeirs of leettrs taeeimnrtd on each end by a non-leettr or
> by one end or oehtr of the input) is daeilpsyd with the iennr leettrs
The last "word" of the above sentence ("leettrs") is incorrect, per the rules
of the challenge. It /should be/ "leertts".
This will make a difference if you feed the first paragraph into your program
and compare the results to the second (transformed) paragraph.
> (i.e. not the first and last leertts) seortd into aaabcehilptl oderr.
> The first and last leertts shloud saty in tehir cceorrt piinoosts. I
> will now daemnorstte waht I maen, uinsg this paaagprrh as input.
[snip]
--
Lew Pitcher
"In Skills, We Trust"
PGP public key available upon request
[toc] | [prev] | [next] | [standalone]
| From | Richard Heathfield <rjh@cpax.org.uk> |
|---|---|
| Date | 2015-11-23 12:21 +0000 |
| Message-ID | <n2v08e$ddi$1@dont-email.me> |
| In reply to | #76796 |
On 21/11/15 19:58, Lew Pitcher wrote:
<snip>
> For those who have taken up Richard's challenge, a note
>
>> Wirte a cceorrt C pagorrm taht rades saadnrtd input and rcdeeoprus it on
>> saadnrtd optuut vabeirtm *ecepxt taht* each word (wchih I define as a
>> cginootuus seeirs of leettrs taeeimnrtd on each end by a non-leettr or
>> by one end or oehtr of the input) is daeilpsyd with the iennr leettrs
>
> The last "word" of the above sentence ("leettrs") is incorrect, per the rules
> of the challenge. It /should be/ "leertts".
Thanks, Lew - you are of course cceorrt.
--
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2015-11-21 21:29 +0000 |
| Message-ID | <87bnanm47t.fsf@bsb.me.uk> |
| In reply to | #76655 |
Richard Heathfield <rjh@cpax.org.uk> writes:
<snip>
> Here's the challenge:
>
> Write a correct C program that reads standard input and reproduces it
> on standard output verbatim *except that* each word (which I define as
> a contiguous series of letters terminated on each end by a non-letter
> or by one end or other of the input) is displayed with the inner
> letters (i.e. not the first and last letters) sorted into alphabetical
> order. The first and last letters should stay in their correct
> positions. I will now demonstrate what I mean, using this paragraph as
> input.
That looked like it was worth a try, so here's a version that (to all
intents and purposes) does away with the arbitrary size limit and allows
for an easy definition of word characters and their collating sequence.
It's terse to the point of obfuscation but then it was just for fun...
S
P
O
I
L
E
R
S
P
A
C
E
S
P
O
I
L
E
R
S
P
A
C
E
S
P
O
I
L
E
R
S
P
A
C
E
S
P
O
I
L
E
R
S
P
A
C
E
#include <stdio.h>
#include <string.h>
const char letters[] = "aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ";
int lnum(int c)
{
const char *pos = strchr(letters, c);
return pos ? pos - letters + 1 : 0;
}
int print_all_but_last_letter_sorted(void)
{
int prev = getchar(), prev_idx = lnum(prev);
if (prev_idx) {
static size_t counts[sizeof letters];
int cur, cur_idx;
while (cur_idx = lnum(cur = getchar())) {
counts[prev_idx] += 1;
prev = cur;
prev_idx = cur_idx;
}
for (size_t i = 1; i < sizeof letters; i++)
while (counts[i]) {
putchar(letters[i-1]);
counts[i] -= 1;
}
putchar(prev);
return cur;
}
return prev;
}
int main(void)
{
int c;
while ((c = getchar()) != EOF)
if (lnum(c)) {
putchar(c);
if ((c = print_all_but_last_letter_sorted()) != EOF)
putchar(c);
}
else putchar(c);
}
And just for the hell of it, in Haskell:
import System.IO
import Data.Char
import Data.List
main = do
content <- getContents
putStr (map_words sort_inner content)
sort_inner [] = []
sort_inner [a] = [a]
sort_inner (first:rest) = first : sort (init rest) ++ [last rest]
map_words f [] = []
map_words f text = f word ++ sep ++ map_words f rest
where (word, tail) = span isAlpha text
(sep, rest) = break isAlpha tail
--
Ben.
[toc] | [prev] | [next] | [standalone]
| From | BartC <bc@freeuk.com> |
|---|---|
| Date | 2015-11-21 22:31 +0000 |
| Message-ID | <n2qr77$u82$1@dont-email.me> |
| In reply to | #76804 |
On 21/11/2015 21:29, Ben Bacarisse wrote: > Richard Heathfield <rjh@cpax.org.uk> writes: >> Here's the challenge: >> >> Write a correct C program that reads standard input and reproduces it >> on standard output verbatim *except that* each word ... > And just for the hell of it, in Haskell: > > import System.IO > import Data.Char > import Data.List > > main = do > content <- getContents > putStr (map_words sort_inner content) > > sort_inner [] = [] > sort_inner [a] = [a] > sort_inner (first:rest) = first : sort (init rest) ++ [last rest] > > map_words f [] = [] > map_words f text = f word ++ sep ++ map_words f rest > where (word, tail) = span isAlpha text > (sep, rest) = break isAlpha tail > OK, another challenge for me: to produce something reasonably succinct but using fairly ordinary code that more people will understand. First attempt here: http://pastebin.com/YAScUbE7 Not complete, because mixed-case word interiors aren't sorted properly. (A few lines to import or declare stuff not included.) I liked your C solution BTW. -- Bartc
[toc] | [prev] | [next] | [standalone]
| From | BartC <bc@freeuk.com> |
|---|---|
| Date | 2015-11-22 10:57 +0000 |
| Message-ID | <n2s6ts$n25$1@dont-email.me> |
| In reply to | #76806 |
On 21/11/2015 22:31, BartC wrote: > On 21/11/2015 21:29, Ben Bacarisse wrote: >> Richard Heathfield <rjh@cpax.org.uk> writes: > >>> Here's the challenge: >>> >>> Write a correct C program that reads standard input and reproduces it >>> on standard output verbatim *except that* each word ... > >> And just for the hell of it, in Haskell: >> sort_inner (first:rest) = first : sort (init rest) ++ [last rest] > OK, another challenge for me: to produce something reasonably succinct > but using fairly ordinary code that more people will understand. > > First attempt here: http://pastebin.com/YAScUbE7 > > Not complete, because mixed-case word interiors aren't sorted properly. It seems your Haskell version doesn't sort mixed case properly either! (As I've just found out after a 7000-file, 750MB download of GHC just to test it. Good thing that was a pre-built binary.) That makes me feel better about my toy language version. But it means the C version is better than either. (Although the specs weren't that clear about the sorting.) -- Bartc
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2015-11-22 13:45 +0000 |
| Message-ID | <87h9kekv0j.fsf@bsb.me.uk> |
| In reply to | #76849 |
BartC <bc@freeuk.com> writes:
> On 21/11/2015 22:31, BartC wrote:
>> On 21/11/2015 21:29, Ben Bacarisse wrote:
>>> Richard Heathfield <rjh@cpax.org.uk> writes:
>>
>>>> Here's the challenge:
>>>>
>>>> Write a correct C program that reads standard input and reproduces it
>>>> on standard output verbatim *except that* each word ...
>>
>>> And just for the hell of it, in Haskell:
>
>>> sort_inner (first:rest) = first : sort (init rest) ++ [last rest]
>
>> OK, another challenge for me: to produce something reasonably succinct
>> but using fairly ordinary code that more people will understand.
>>
>> First attempt here: http://pastebin.com/YAScUbE7
>>
>> Not complete, because mixed-case word interiors aren't sorted properly.
>
> It seems your Haskell version doesn't sort mixed case properly either!
Yes, I should have said that the two were not the same. I wrote the
Haskell first, then a simple C version that used qsort. Finally I
decided to make a more flexible C version where it was natural to choose
an ordering. I should probably have chosen
const char letters[] =
"ABCDEFGHIJKLMNOPQRSTUVZXYZabcdefghijklmnopqrstuvzxyz";
to make all the version the same. To change the Haskell, replace "sort"
with "sortBy (comparing toLower)" and import Data.Ord to get the
comparing combinator. (It's a one-liner, as is a caseless compare
function, but the Haskell way is to use what's there).
They are still not the same though because the Haskell version is
Unicode aware (except for the simple sort).
--
Ben.
[toc] | [prev] | [next] | [standalone]
Page 1 of 4 [1] 2 3 4 Next page →
Back to top | Article view | comp.lang.c
csiph-web