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
------------------------------------
Porque la �nica lucha que se pierde, es la que se abandona,
�Ni un paso atr�s!
module Parser where
parser xs = parser0 (words xs)
parser0 [] = []
parser0 (x:xs) = ifNegative x:parser0 xs
ifNegative (x:xs) = if (x == '-') then (negative xs) else (nonNegative (x:xs))
negative xs = (0 - fst (readDec xs!!0))
nonNegative xs = fst (readDec xs!!0)
import InputOutput
import Functions
import Parser
-- Function Main
--main = control (parser (input "twoboxes.dat"))
--je = do {c <- parser "12 21"; return c}
-- Testes
test = " 3 4 7 2 5 3 54\n 7 10 9 10 3 1 600\n10 9 4 8 1 6 312\n 3 9
5 1 6 4 111\n 4 3 2 7 6 1 -18\n 3 3 1 9 5 4 -171\n 5 9 9 1 7 6 36
3\n 1 2 9 3 9 2 -36\n 2 6 8 2 6 10 -24\n 8 1 10 7 5 1 45\n"
test':: Num a => [a]
test' =
[3,4,7,2,5,3,54,7,10,9,10,3,1,600,10,9,4,8,1,6,312,3,9,5,1,6,4,111,4,3,2,7,6,1,-18,3,3,1,9,5,4,-171,5,9,9,1,7,6,36,3,1,2,9,3,9,2,-36,2,6,8,2,6,10,-24,8,1,10,7,5,1,45]
test1 = control [3,-3,-7,-2,-5,-13,54,7,10,9,10,3,1,600,1,1,1,1,1,1,6]
test2 = control [1,2,3]
escritura = output "output" test1
letura = input "output"
arch = input "twoboxes.dat"
arch2 = readFile "twoboxes.dat"
module InputOutput where
import IO
import Parser
-- OUTPUT
output outfile result = do
writeFile outfile (show result)
putStr ("Writing in file " ++ "'" ++ outfile ++ "'"
++ " done.\n")
-- INPUT
input infile = do
hdl <- openFile infile ReadMode
content <- hGetContents hdl
return content
module Functions where
map' f [] = []
map' f (t:u:v:w:x:y:z:xs) = f t u v w x y z:map' f xs
operation a b c d e f g = do
r <- (out [a,b,c,d,e,f])
if (r == g) then
return r else return (abs (r-g))
control xs
| (mod (length xs) 7) == 0 = map' operation' xs
| otherwise = error "Incorrect size of vector"
-- FUNCTION 'OUT'
out [a, b, c, d, e, f] = [ divicao [divicao [soma [resta [soma [a,b], c], d], e], f]
]
-- FUNCTION DECLARATION
soma :: [Float] -> Float
soma [x, y] = x + y
resta :: [Float] -> Float
resta [x, y] = x - y
divicao :: [Float] -> Float
divicao [a, b]
| b == 0 = error "Division by zero"
| otherwise = a / b
operation' a b c d e f g = out [a,b,c,d,e,f]