home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / function / 1483 < prev    next >
Encoding:
Text File  |  1992-12-25  |  3.1 KB  |  122 lines

  1. Newsgroups: comp.lang.functional
  2. Path: sparky!uunet!munnari.oz.au!cs.mu.OZ.AU!munta.cs.mu.OZ.AU!fjh
  3. From: fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON)
  4. Subject: Re: Completely functional program in C
  5. Message-ID: <9236105.4381@mulga.cs.mu.OZ.AU>
  6. Sender: news@cs.mu.OZ.AU
  7. Organization: Computer Science, University of Melbourne, Australia
  8. References: <1hakebINNdsa@rave.larc.nasa.gov>
  9. Date: Fri, 25 Dec 1992 18:02:01 GMT
  10. Lines: 110
  11.  
  12. goodrich@lynx.larc.nasa.gov (Mike Goodrich) writes:
  13.  
  14. >Greetings,
  15. >    The following is an example of a functional program in C I wrote after
  16. >reading MaClannan's book on FP. I would like to invite comments from the FP
  17. >community.
  18.  
  19. I would not describe this program as particularly "functional".
  20. It does have a lot of tail recursion, but there is not much else that
  21. could be called functional.
  22.  
  23. [...]
  24. >main(argc, argv)
  25. >    int             argc;
  26. >    char          **argv;
  27. >{
  28. >    unsigned char   buff[REC_SIZE];
  29. >
  30. >    if (argc < 2) {
  31. >        printf("\nUsage: hexdmp <filename>\n");
  32. >        exit(0);
  33. >    }
  34. >    DumpFile(fopen(argv[1], "rb"), buff);
  35. >}
  36.  
  37. - exit(0) is not very functional
  38. - failing to return a value from main() is not very functional
  39.   (not to mention that it's not very good C either ;-)
  40. - passing uninitialized parameters (buff) is not very functional
  41.  
  42. >void
  43. >PutTextLine(NumRead, buff)
  44. >    short           NumRead;
  45. >    unsigned char  *buff;
  46. >{
  47. >    switch (NumRead) {
  48. >    case 0:
  49. >        return;
  50. >    default:
  51. >        if (*buff < ' ' || *buff > '~') {    /* printable ? */
  52. >            printf(".");
  53. >        } else {
  54. >            printf("%c", *buff);
  55. >        }
  56. >        PutTextLine(NumRead - 1, buff + 1);
  57. >    }
  58. >}
  59.  
  60. Why the switch statements? What's wrong with using if statements?
  61.  
  62. Also, purely functional programs should have declarative I/O.
  63. Since this is impossible, the next best thing is to confine all your
  64. I/O to the very top-level of your program, or alternately to pass
  65. pretend state-of-the-world arguments to and from a library of declarative
  66. I/O functions (OK, admittedly that's a bit silly in this context, but
  67. then it's a pretty silly context ;-)
  68.  
  69. >
  70. >void
  71. >PutHexLine(NumRead, buff)
  72. >    short           NumRead;
  73. >    unsigned char  *buff;
  74. >{
  75. >    switch (NumRead) {
  76. >    case 0:
  77. >        printf("\t");
  78. >        return;
  79. >    default:
  80. >        printf("%02x ", *buff);
  81. >        PutHexLine(NumRead - 1, buff + 1);
  82. >    }
  83. >}
  84. >
  85. >void
  86. >DoLine(NumRead, buff, count, fp)
  87. >    short           NumRead, count;
  88. >    unsigned char  *buff;
  89. >    FILE           *fp;
  90. >{
  91. >    switch (NumRead) {
  92. >    case 0:
  93. >        return;
  94. >    default:
  95. >        printf("\n<%d>\t", count * REC_SIZE);
  96. >        PutHexLine(NumRead, buff);
  97. >        PutTextLine(NumRead, buff);
  98. >        DoLine(fread(buff, 1, REC_SIZE, fp), buff, count + 1, fp);
  99. >    }
  100. >}
  101. >
  102. >void 
  103. >DumpFile(fp, buff)
  104. >    FILE *fp;
  105. >    unsigned char  *buff;
  106. >{
  107. >    if (fp == NULL) {
  108. >        printf("Error: cannot open input file\n");
  109. >        exit(-1);
  110. >    }
  111. >    DoLine(fread(buff, 1, REC_SIZE, fp), buff, 0, fp);
  112. >}
  113.  
  114. fread() is not a very functional function, since the buff array is 
  115. effectively passed by reference rather than by value.
  116.  
  117. -- 
  118. Fergus Henderson             fjh@munta.cs.mu.OZ.AU      
  119. This .signature virus is a self-referential statement that is true - but 
  120. you will only be able to consistently believe it if you copy it to your own
  121. .signature file!
  122.