Isn't this tail recursive?

Ronny Wichers Schreur ronny@cs.kun.nl
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 <http://www.haskell.org/onlinereport/decls.html#sect4.2.1>,
"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)?
        where
            countAll' [] _ counts
                =  counts
            countAll' (c:cs) newWord (Counts nl nw nc)
                =   case charKind c of
                        Normal
                            -> countAll' cs 0 (Counts nl (nw+newWord) (nc+1))
                        White
                            -> countAll' cs 1 (Counts nl nw (nc+1))
                        Newline
                            -> 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`.



Cheers,

Ronny Wichers Schreur