home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / c / 12474 < prev    next >
Encoding:
Text File  |  1992-08-18  |  2.1 KB  |  54 lines

  1. Xref: sparky comp.lang.c:12474 comp.programming:2367
  2. Newsgroups: comp.lang.c,comp.programming
  3. Path: sparky!uunet!nntp1.radiomail.net!fernwood!metrop!robert
  4. From: robert@metropolis.com (Robert Munyer)
  5. Subject: Re: A LITTLE BRAINTEASER...
  6. Message-ID: <Bt6BK2.HrC@metropolis.com>
  7. Organization: Metropolis Software, Inc.
  8. References: <1992Aug14.123054.4872@fwi.uva.nl> <9222810.19441@mulga.cs.mu.OZ.AU> <O1ywr*J00@ro-chp.UUCP>
  9. Date: Tue, 18 Aug 1992 09:55:14 GMT
  10. Lines: 42
  11.  
  12. In article <O1ywr*J00@ro-chp.UUCP> chopps@ro-chp.UUCP (Chris Hopps) writes:
  13.  
  14. > I guess I don't understand what it is you exactly want.  Should the program
  15. > be portable?  What _is_ a data structure to you?  To me recursing and
  16. > saving values on the stack is a data structure, albeit an ugly and not very
  17. > usefull one in this case, it still represents a logical structuring of data.
  18.  
  19. I think what he wanted was to use only data structures that the compiler
  20. creates AUTOMATICALLY, without being asked, in order to implement the
  21. semantics of the language.  And "ugly" is in the eye of the beholder.
  22.  
  23. The problem is pretty easy to solve in any language that supports recursion
  24. within multiple threads.  C does not support this, but the Unix environment
  25. does.  Compile these two programs, and then pipe either one into the other:
  26.  
  27. -------------------- a.c --------------------
  28. #include <stdio.h>
  29. f ()
  30. { int c = getchar ();
  31.   return c == EOF ? 0 :
  32.          f () || c != '\n' ? (putchar (c), 1) : 1;
  33. }
  34. main () { f (); putchar ('\n'); }
  35. -------------------- b.c --------------------
  36. #include <stdio.h>
  37. f ()
  38. { int c = getchar ();
  39.   return c == EOF ? 0 :
  40.          c == '\n' ? 1 :
  41.          f () ? (putchar (c), 1) :
  42.          (putchar (c), 0);
  43. }
  44. main () { while (f ()) putchar ('\n'); }
  45. ---------------------------------------------
  46.  
  47. At the shell prompt, you can type either "a | b" or "b | a".  Note that each
  48. process has one recursive function, with one local variable and no arguments.
  49.  
  50. ______________________________________________________________________
  51. Robert Munyer            |   "I may be synthetic, but I'm not stupid."
  52. robert@metropolis.com    |      -- Bishop, _Aliens_
  53.  
  54.