title: Kernighan & Ritchie word count example program in a functional language
body: <p>I have been reading a little bit about functional programming on the web lately and I think I got a basic idea about the concepts behind it.</p>
  
  <p>I'm curious how everyday programming problems which involve some kind of state are solved in a pure functional programing language.</p>
  
  <p>For example: how would the word count program from the book 'The C programming Language' be implemented in a pure functional language?</p>
  
  <p>Any contributions are welcome as long as the solution is in a pure functional style.</p>
  
  <p>Here's the word count C code from the book:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  #define IN  1 /* inside a word */
  #define OUT 0 /* outside a word */
  
  /* count lines, words, and characters in input */
  main()
  {
    int c, nl, nw, nc, state;
  
    state = OUT;
    nl = nw = nc = 0;
    while ((c = getchar()) != EOF) {
      ++nc;
      if (c == '\n')
        ++nl;
      if (c == ' ' || c == '\n' || c = '\t')
        state = OUT;
      else if (state == OUT) {
        state = IN;
        ++nw;
      }
    }
  
    printf("%d %d %d\n", nl, nw, nc);
  }
  </code></pre>