
hi, I'm trying to figure out how to parse a text file, but these type-conversations are just killing me! Whats wrong with following code : module Main where main = putStrLn "hi" parse str = take 10 str load = do content <- readFile "parse.hs" return content when try : Prelude> parse load <interactive>:1:6: Couldn't match `[a]' against `IO String' Expected type: [a] Inferred type: IO String In the first argument of `parse', namely `load' In the definition of `it': it = parse load i tried different combinations, but still cant figure out... How do u use an "IO String" in normal "String" functions.. I want to particulary slurp file in a string and then make funny things with it...spliting to lines,parsing them and building hash like strucutre of it...etc. later i want to try spliting,regex-es..etc. It is very hard to figure out how to use these modules.. :"(

On Thu, 1 Dec 2005, raptor wrote:
Whats wrong with following code :
module Main where
main = putStrLn "hi"
parse str = take 10 str
load = do content <- readFile "parse.hs" return content
Nothing! It works, output is "hi". "load" isn't used, but its type would be "load :: IO String", so if you want to use it: main = do content <- load putStrLn (show content)
i tried different combinations, but still cant figure out... How do u use an "IO String" in normal "String" functions.. I want to particulary slurp file in a string and then make funny things with it...spliting to lines,parsing them and building hash like strucutre of it...etc.
Well, "main" is an example, and actually "load" is another example - you don't really do much with your value in "load", but you would find that the value has type String there. The catch is that of course you must use this notation (<-), and whatever "load" returns will be IO something. But you can use that in main, etc. This may not make much sense without a little more exposure to the underlying principles, but if you're just trying different combinations, it should give you some ideas. By the way, in your style of writing, does "..." end a sentence, or should that be interpreted in some other way? Donn Cave, donn@drizzle.com

thanx all for the quick answers, tommorow will try them :) |By the way, in your style of writing, does "..." end a |sentence, or should that be interpreted in some other way? ]- "..." <- mostly unspoken things, which i think at the moment, but dont want to go in details, 'cause the question will become blurred. It is the first time somebody ask me about this :"), and I'm doing it all the time mechanicaly w/o ever thinking...hmm funny...or i should call it "lazy", go figure :").

raptor
i tried different combinations, but still cant figure out... How do u use an "IO String" in normal "String" functions.. I want to particulary slurp file in a string and then make funny things with it...spliting to lines,parsing them and building hash like strucutre of it...etc.
http://www.haskell.org/hawiki/ThatAnnoyingIoType This wiki page has useful information about how to deal with the IO type. -- Shae Matijs Erisson - http://www.ScannedInAvian.com/ - Sockmonster once said: You could switch out the unicycles for badgers, and the game would be the same.
participants (3)
-
Donn Cave
-
raptor
-
Shae Matijs Erisson