
The problem you are trying to come to grips with here is one of the watersheds in the learning curve that most people learning Haskell encounter, namely how to use values returned in Monads. I could point out that you have already solved the same problem when you used getLine. The question you need to ask yourself is not, "how do I convert a value of type IO String to a value of type String", but "how do I use the result of a calculation within the IO Monad". Look at where you've already solved the problem, and the answer will appear almost immediately. Robin -----Original Message----- From: Michael Ruth [mailto:meruth@hotmail.com] Sent: Friday, 15 March 2002 11:30 To: haskell-cafe@haskell.org Subject: Date Sorting Round Three long set of code here, I think I figured out what you all were telling me. Compose small functions then compose bigger functions from smaller functions and so on... The problem I am having is I have an (IO String) where I need a (String) and it reports it as such: ERROR "C:\Documents and Settings\drunkenmike\My Documents\Programming\Csci4501\H askell\datesort.hs":41 - Type error in application *** Expression : separateIntoLines (getInput x) *** Term : getInput x *** Type : IO String *** Does not match : [Char] I am gonna mark line 41 with a * at the beginning of the line. I have been looking high and low for the answer to IOString -> String.... all I think I need to do (I think anyways) By the way, thanks to everyone for their help. import IO --- -- Control Flow --- datesort :: IO() datesort = do { putStr "Enter a filename: "; theFile <- getLine; sortAndOutput (readInAndGetReady theFile); } -- -- Composite functions -- 1) Read in & Get ready to sort -- 2) sort & output -- -- readInAndGetReady :: String -> [String] *readInAndGetReady x = fixDates (makeSortableDates (linesToDates (separateIntoLines (getInput x)))) sortAndOutput :: [String] -> IO() sortAndOutput x = writeTheFile (getOutput (makePrintableDates (qsort x))) -- -- Make the date printable. Printable means a string consisting of -- dates in the form dd mm yyyy (more or less undoes the make sortable f x) -- makePrintableDates :: [String] -> [String] makePrintableDates x = map makePrintableDate x -- -- Make the date printable. Printable means a string consisting of -- dates in the form dd mm yyyy (more or less undoes the make sortable f x) -- makePrintableDate :: String -> String makePrintableDate x = unwords (reverse (words x)) -- -- Make all the dates sortable. Sortable means a string consisting of -- dates in the form yyyy mm dd -- makeSortableDates :: [[String]] -> [String] makeSortableDates x = map makeSortableDate x -- -- Make the date sortable. Sortable means a string consisting of -- dates in the form yyyy mm dd -- makeSortableDate :: [String] -> String makeSortableDate x = unwords (reverse x) -- -- Get the input from the given file -- getInput :: String -> IO String getInput myfile = readFile myfile -- -- Seperate the input file into an list of Strings -- separateIntoLines :: String -> [String] separateIntoLines str = lines str -- -- Transform all the lines into dates -- linesToDates :: [String] -> [[String]] linesToDates x = map lineToDate x -- -- Transform the lines into dates -- lineToDate :: String -> [String] lineToDate x = words x -- -- Transform the dates into lines -- getOutput :: [String] -> String getOutput x = unlines x -- -- Fix all the dates so that they are in the form 0x if x < 10 -- fixDates:: [String] -> [String] fixDates x = map fixPrefixes x -- -- Fix the dates so that they are in the form 0x if x < 10 -- fixPrefixes :: String -> String fixPrefixes p | p == "1" = "01" | p == "2" = "02" | p == "3" = "03" | p == "4" = "04" | p == "5" = "05" | p == "6" = "06" | p == "7" = "07" | p == "8" = "08" | p == "9" = "09" | otherwise = p -- -- Quicksort the List of strings -- qsort :: [String] -> [String] qsort [] = [] qsort (x:xs) = qsort elts_lt_x ++ [x] ++ qsort elts_greq_x where elts_lt_x = [y | y <- xs, y < x] elts_greq_x = [y | y <- xs, y >= x] -- -- Write the ouput string to a file -- writeTheFile :: String -> IO() writeTheFile x = writeFile "output.txt" x _________________________________________________________________ Chat with friends online, try MSN Messenger: http://messenger.msn.com _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (1)
-
Garner, Robin