I am sorry for, maybe, a silly question. f name = readFile name >>= putStr has the type String -> IO () And I need String -> String, 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. Thank you in advance for the help. ----------------- Serge Mechveliani mechvel@botik.ru
"S.D.Mechveliani" <mechvel@math.botik.ru> writes:
I am sorry for, maybe, a silly question.
f name = readFile name >>= putStr
has the type String -> IO () And I need String -> String,
Then you're out of luck. You can't get at the contents of the file named "name" without doing IO. You can't do IO outside of the IO monad. And once in, you never get out of it, it contaminates everything up to the top level. Purity sucks. :-) Of course, you can do all that with unsafePerformIO, but you probably shouldn't. What you probably can do, is something like main = do -- some actions or functions to get the value of 'name', either: name <- <actions> -- or: let name = <a pure function> contents <- readFile name -- actions or functions that use 'contents' as a normal string putStr (show $ tails contents) -- or whatever -kzm -- If I haven't seen further, it is by standing in the footprints of giants
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
participants (3)
-
ketil@ii.uib.no -
Lars Lundgren -
S.D.Mechveliani