Isn't this tail recursive?

Ronny Wichers Schreur
Fri, 15 Mar 2002 19:27:32 +0100

Another way to evaluate the accumulating counts in each
call is to combine them in a data type and use strictness
flags for the individual count fields.

(See <>,
"Strictness Flags")

For example (untested):

    data Counts = Counts !Int !Int !Int

    countAll :: String -> Counts
    countAll str
        =   countAll' str 0 (Counts 0 0 0)
                          // shouldn't this be 1 (start with new word)?
            countAll' [] _ counts
                =  counts
            countAll' (c:cs) newWord (Counts nl nw nc)
                =   case charKind c of
                            -> countAll' cs 0 (Counts nl (nw+newWord) (nc+1))
                            -> countAll' cs 1 (Counts nl nw (nc+1))
                            -> countAll' cs 1 (Counts (nl+1) nw (nc+1))

The function countAll' is now strict in its counts argument. Because of the strictness flags in Counts, each count is evaluated.

I think this looks nicer than using local calls to `seq`.


Ronny Wichers Schreur