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 6891361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:24:01+00:00 2026-05-27T06:24:01+00:00

So I’ve read the theory, now trying to parse a file in Haskell –

  • 0

So I’ve read the theory, now trying to parse a file in Haskell – but am not getting anywhere. This is just so weird…

Here is how my input file looks:

        m n
        k1, k2...

        a11, ...., an
        a21,....   a22
        ...
        am1...     amn

Where m,n are just intergers, K = [k1, k2...] is a list of integers, and a11..amn is a “matrix” (a list of lists): A=[[a11,...a1n], ... [am1... amn]]

Here is my quick python version:

def parse(filename):
    """
    Input of the form:
        m n
        k1, k2...

        a11, ...., an
        a21,....   a22
        ...
        am1...     amn

    """

    f = open(filename)
    (m,n) = f.readline().split()
    m = int(m)
    n = int(n)

    K = [int(k) for k in f.readline().split()]

    # Matrix - list of lists
    A = []
    for i in range(m):
        row = [float(el) for el in f.readline().split()]
        A.append(row)

    return (m, n, K, A)

And here is how (not very) far I got in Haskell:

import System.Environment
import Data.List

main = do
    (fname:_) <- getArgs
    putStrLn fname --since putStrLn goes to IO ()monad we can't just apply it
    parsed <- parse fname
    putStrLn parsed

parse fname = do
    contents <- readFile fname
    -- ,,,missing stuff... ??? how can I get first "element" and match on it?

    return contents

I am getting confused by monads (and the context that the trap me into!), and the do statement. I really want to write something like this, but I know it’s wrong:

firstLine <- contents.head
(m,n) <- map read (words firstLine)

because contents is not a list – but a monad.

Any help on the next step would be great.

So I’ve just discovered that you can do:

 liftM lines . readFile

to get a list of lines from a file. However, still the example only only transforms the ENTIRE file, and doesn’t use just the first, or the second lines…

  • 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-27T06:24:01+00:00Added an answer on May 27, 2026 at 6:24 am

    The very simple version could be:

    import Control.Monad (liftM)
    
    -- this operates purely on list of strings
    -- and also will fail horribly when passed something that doesn't 
    -- match the pattern
    parse_lines :: [String] -> (Int, Int, [Int], [[Int]])
    parse_lines (mn_line : ks_line : matrix_lines) = (m, n, ks, matrix)
        where [m, n] = read_ints    mn_line
              ks     = read_ints    ks_line
              matrix = parse_matrix matrix_lines
    
    -- this here is to loop through remaining lines to form a matrix
    parse_matrix :: [String] -> [[Int]]
    parse_matrix lines = parse_matrix' lines []
        where parse_matrix' []       acc = reverse acc
              parse_matrix' (l : ls) acc = parse_matrix' ls $ (read_ints l) : acc
    
    -- this here is to give proper signature for read
    read_ints :: String -> [Int]
    read_ints = map read . words
    
    -- this reads the file contents and lifts the result into IO
    parse_file :: FilePath -> IO (Int, Int, [Int], [[Int]])
    parse_file filename = do
        file_lines <- (liftM lines . readFile) filename
        return $ parse_lines file_lines
    

    You might want to look into Parsec for fancier parsing, with better error handling.

    *Main Control.Monad> parse_file "test.txt"
    (3,3,[1,2,3],[[1,2,3],[4,5,6],[7,8,9]])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
i want to parse a xhtml file and display in UITableView. what is the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is

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.