Note that there are some explanatory texts on larger screens.

plurals
  1. PODifficulty in Haskell with recursive tuple function
    text
    copied!<p>I have a small program that reads in a file and processes the data into a custom data type. The file being read contains lines of data that look like this:</p> <pre><code>cat 10 20 dog hamster 12 2 wombat monkey 1 9 zebra lion 30 60 rhino ... </code></pre> <p>My program to process the file looks like this:</p> <pre><code>main :: IO () main = do contents &lt;- readFile "myfile.xyz" let process = clean contents let f = processFile process print f clean :: String -&gt; [[String]] clean x = Prelude.map words $ lines x processFile :: [[String]] -&gt; [(XyzData)] processFile [[a,b,c,d]] = [(XyzData a (read b :: Int) (read c :: Int) d)] processFile (x:xs) = processFile xs data XyzData = XyzData { animal1 :: String, number1 :: Int, number2 :: Int, animal2 :: String } deriving (Show) </code></pre> <p>My problem is with the <code>processFile</code> function. Currently, this function only captures the last line of the file and prints it to the screen. I am confused on how to implement recursion with a tuple instead of using append with a list in this function. Can anyone show me how how to fix my function and/or a better implementation of this function? The printed output of this program should be:</p> <pre><code>[XyzData {animal1 = "cat", number1 = 10, number2 = 20, animal2 = "dog"}, [XyzData {animal1 = "hampster", number1 = 12, number2 = 2, animal2 = "wombat"}, [XyzData {animal1 = "monkey", number1 = 1, number2 = 9, animal2 = "zebra"}, [XyzData {animal1 = "lion", number1 = 30, number2 = 60, animal2 = "rhino"}] </code></pre> <p>Thanks for your time. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload