String vs IO ()

Lars Lundgren lars@prover.com
Wed, 23 Jan 2002 11:30:59 +0100 (CET)


On Wed, 23 Jan 2002, S.D.Mechveliani wrote:

> I am sorry for, maybe, a silly question.
> 
>   f name = readFile name >>= putStr
> 
> has the type  String -> IO ()
> And I need    String -> String,
> 

But that IS not the type of a function returning the contents of a file!

(I'm sure you know what a mathematical function from String to String is
and what it isn't, and the function you want just isnt String -> String,
but rather (String,FileSystem) -> String, or String -> (FileSystem ->
String) or String -> IO String. )

You want to have an apple, but you insist on wanting to call it a banana.
Why? If you are going to be happy with haskell, you have to get used to
call things what they are.

> so that  (f name)  is a string contained in the given file.
> 
> I never learned such things in Haskell because they are rather 
> non-functional.
> 

Not really. Everything in Haskell is 100% functional, but readFile
"foo.txt" is not a String, it is an action that when executed returns a
string. Sequences of actions can be built whith >>= (or do { ; ; ;} )
(Since actions belong to a Monad).

Mvh
/Lars L