toString for lists

G.W.M. Vissers vissers@theochem.kun.nl
Mon, 31 May 1999 10:55:33 +0200 (MET DST)


Hello,

I was doing some debugging when I noticed how unfortunate the standard
toString for lists is. This toString function is defined in StdList as:

instance toString [x] | toChar x
        where
        toString::![x] -> {#Char} | toChar x
        toString xs = ltosacc xs ""
        where
                ltosacc [h:t] acc = ltosacc t (acc +++ toString (toChar
h))
                ltosacc []        acc = acc

The problem is that this method only works for lists of elements that can
be converted into characters. For most types a toString is predefined, but
not a toChar. Besides I don't see the use of first converting the element
into a character, and then making a string out of it. Would'n it be a lot
easier to convert the elemnts of the list into Strings directly, like:

instance toString [x] | toString x       
 where
        toString::![x] -> {#Char} | toString x
        toString xs = ltosacc xs ""
        where
                ltosacc [h:t] acc = ltosacc t (acc +++ toString h)
                ltosacc []        acc = acc

Yours,

G