
You probably want to do something like this: main = do { contents <- input "twoboxes.dat" return (control (parser contents)) } At 11:53 25-02-02 -0300, Juan M. Duran wrote:
Hi, I'm writting a small parser in Haskell and, when it is all done, I get the following problem: Type Binding. The thing is, I have 3 main functions: 1) Read the file, its type is: [Char] ->IO [Char] (see InputOutput.hs) 2) Parse a string (using words and readDec), its type is: Integral a => [Char] -> [a] (see Parse.hs) 3) Parse a list of integer, its type is: [Float] -> [[Float]] (Functions.hs)
Now the problem is that I cannot run the first function, then use its results as an input of the second function and, finally, its results as the input of the third function.
How can I fix this without modifing all my functions because they, independly, works fine.
Juan

I thing that wont works, look: contents :: IO [Char] parser :: Integral a => [Char] -> [a] control :: [Float] -> [[Float]] The two problems are: 1) The input of parser. Doesnt match with the type of input 2) The input of control (or the output of parser). Doesn match with the type of the next function. On Mon, 25 Feb 2002, Rijk J. C. van Haaften wrote:
You probably want to do something like this:
main = do { contents <- input "twoboxes.dat" return (control (parser contents)) }
At 11:53 25-02-02 -0300, Juan M. Duran wrote:
Hi, I'm writting a small parser in Haskell and, when it is all done, I get the following problem: Type Binding. The thing is, I have 3 main functions: 1) Read the file, its type is: [Char] ->IO [Char] (see InputOutput.hs) 2) Parse a string (using words and readDec), its type is: Integral a => [Char] -> [a] (see Parse.hs) 3) Parse a list of integer, its type is: [Float] -> [[Float]] (Functions.hs)
Now the problem is that I cannot run the first function, then use its results as an input of the second function and, finally, its results as the input of the third function.
How can I fix this without modifing all my functions because they, independly, works fine.
Juan

I thing that wont works, look: contents :: IO [Char]
parser :: Integral a => [Char] -> [a]
control :: [Float] -> [[Float]]
The two problems are: 1) The input of parser. Doesnt match with the type of input 2) The input of control (or the output of parser). Doesn match with the type of the next function.
The "do" notation used by Rijk fixes the first problem; you should try it.
For the second problem, you want to convert an Integral to a Float - but are you sure? Integers aren't floating point numbers!
If you are sure, then do something like
main =
do {
contents <- input "twoboxes.dat"
return (control (map fromInteger (parser contents)))
}
fromInteger has the type Num a => Integer -> a, and since Num Float and Integral Integer, all your type constraints will be satisfied.
HTH.
--KW 8-)
--
Keith Wansbrough

The problem is fixed. I fixed it using the do notation as Rijk said, it worked perfectly. Now I got two more problems: 1) Should I use the do notation in order to write this result (type IO [[Float]]) to a file? How? 2) The Glasglow compiler doesn let me compile one library because I use the function readFloat (declare in the Prelude of Hugs 98), but Hugs lets me, why? Thanks On Mon, 25 Feb 2002, Keith Wansbrough wrote:
I thing that wont works, look: contents :: IO [Char]
parser :: Integral a => [Char] -> [a]
control :: [Float] -> [[Float]]
The two problems are: 1) The input of parser. Doesnt match with the type of input 2) The input of control (or the output of parser). Doesn match with the type of the next function.
The "do" notation used by Rijk fixes the first problem; you should try it.
For the second problem, you want to convert an Integral to a Float - but are you sure? Integers aren't floating point numbers!
If you are sure, then do something like
main = do { contents <- input "twoboxes.dat" return (control (map fromInteger (parser contents))) }
fromInteger has the type Num a => Integer -> a, and since Num Float and Integral Integer, all your type constraints will be satisfied.
HTH.
--KW 8-) -- Keith Wansbrough
http://www.cl.cam.ac.uk/users/kw217/ University of Cambridge Computer Laboratory.

I got a function with type :: IO [[Double]], and what I want is write this output in a file, how can I do it... I mean, I cannot doit by just using writeFile.... And one more thing: the Glasglow compiler doesn let me compile because I use the function readFloat (declare in the Prelude of Hugs 98), but Hugs lets me, why? How can I solve it? Thanks

On Wed, 27 Feb 2002, Juan M. Duran wrote:
I got a function with type :: IO [[Double]], and what I want is write this output in a file, how can I do it... I mean, I cannot doit by just using writeFile.... (snip)
Does something like this help at all? myfn :: IO [[Double]] myfn = return [[1.346, 4.144], [5.143, 2.453]] format_doubles :: [[Double]] -> String format_doubles x = foldr (++) "" (map format_line x) format_line :: [Double] -> String format_line [] = "\n" format_line x = foldr1 (\x y -> x ++ ", " ++ y) (map show x) ++ "\n" main = myfn >>= (\x -> return $ format_doubles x) >>= putStr Okay, it's not the most readable bit of code, but I'm guessing it covers the bit that's confusing you. All the best, Mark

On Wed, 27 Feb 2002, Juan M. Duran wrote:
I got a function with type :: IO [[Double]], and what I want is write this output in a file, how can I do it... I mean, I cannot doit by just using writeFile....
How about.. do double_list_result <- your_function writefile "/file/path/foo" (show double_list_result)
And one more thing: the Glasglow compiler doesn let me compile because I use the function readFloat (declare in the Prelude of Hugs 98), but Hugs lets me, why? How can I solve it?
a solution is myreadfloat string = (read string) :: Float (assuming readFloat is a function String -> Float and not something like Handle -> IO Float) Jay Cox
participants (5)
-
Jay Cox
-
Juan M. Duran
-
Keith Wansbrough
-
Mark Carroll
-
Rijk J. C. van Haaften