[Haskell-cafe] Useful: putCharLn {inspire by the Int->[Char] thread

Gene A yumagene at gmail.com
Sat Aug 19 13:21:36 EDT 2006

The thread on the use of "show" and print to display an Int value,
brought up a problem I had early on... the one of cleanly displaying a
Char value, on a line all by itself.. My first attempts:

This was just plain hard to read: with the character t being where it was:

Prelude> putChar $ head "this and that"
So I tried this and of course ... type mismatch:

Prelude> putStrLn $ head "this and that"
    Couldn't match `String' against `Char'
      Expected type: [String]
      Inferred type: [Char]
    In the first argument of `head', namely `"this and that"'
    In the second argument of `($)', namely `head "this and that"'
So I did this... to manually turn it to a string and it does work, but
a little cumbersome to work into other function calls:
Prelude> putStrLn $ (head "this and that"):[]
so the definition of putCharLn came to life {may be in some library already
and I just haven't found it yet.. but it is in my toolbox now}:

Prelude> let putCharLn c = putStrLn (c:[])
and an application of it:

Prelude> putCharLn $ head "this and that"
now I also have the char to string conversion alone:

c2Str c = c:[]

Prelude> let c2Str c = c:[]
Prelude> c2Str 'A'
Now this is almost too trivial a thing to mention these gizmos...
what with all the monadic constructions that greater minds  toss
about on this list.. and I am trying to get understanding of that
still, but ....
sometimes we just accept the unacceptable little irritants
rather than just code a solution, no matter how simple it is.
There are probably troves of simple workarounds out there
that seem too trivial to mention but hey, share 'em...
might hit a guy like me that says. "now why didn't I think to do that?"

happy days,

More information about the Haskell-Cafe mailing list