Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7697109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:56:04+00:00 2026-05-31T21:56:04+00:00

I have a small program that reads in a file and processes the data

  • 0

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:

cat    10    20    dog
hamster    12    2    wombat
monkey    1    9    zebra
lion    30    60    rhino
...

My program to process the file looks like this:

main :: IO ()
    main = do
    contents <- readFile "myfile.xyz"
    let process = clean contents
    let f = processFile process
    print f

clean :: String -> [[String]]
clean x = Prelude.map words $ lines x

processFile :: [[String]] -> [(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)

My problem is with the processFile 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:

[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"}]

Thanks for your time.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T21:56:05+00:00Added an answer on May 31, 2026 at 9:56 pm

    You probably intended

    processFile :: [[String]] -> [(XyzData)]
    processFile ([a,b,c,d]:xs) = (XyzData a (read b :: Int) (read c :: Int) d) : processFile xs
    processFile (x:xs) = processFile xs 
    processFile [] = []
    

    Your first pattern [[a,b,c,d]] matches only lists with exactly one element, which is a list with exactly four elements.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small java program that reads a file in, in eclipse i
I have a program that creates a small file in the Bin directory for
Let say I have written a small program that reads file_A and file_B as
Background I have a Spring batch program that reads a file (example file I
I have a small SOAP CGI gatewqay C++ program that reads an HTTP SOAP
I have a small C++ program that parses a file given as a command
I have a small server program that accepts connections on a TCP or local
I have a small command line program that uses the Team System API. When
I have a Java program that runs many small simulations. It runs a genetic
I have a small program <350ish lines of code on 5 forms>. It works

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.