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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:06:31+00:00 2026-05-31T12:06:31+00:00

I am solving Project Euler Problem 11 . I have copy-pasted the table in

  • 0

I am solving Project Euler Problem 11. I have copy-pasted the table in the problem into a file called “input.txt”. Each line of the input file contains a row of a 20×20 matrix, and the columns are separated by whitespace.

What I want is a function that reads this file, and returns it as an IO Array. I’m having huge trouble doing this.

So far I’ve done this:

import System.IO
import Control.Monad

main = readFile "input.txt"

This of course only gives me the IO String representation of the input file, but everything I try seems to fail. How should I proceed? I know I should do something like

array ((1,1),(20,20)) [ the numbers tupled with their indices ]

but converting the numbers is outright impossible for me, most likely because I do not entirely understand monads yet.

I am confident that this is in fact quite easy, once you understand it.

Does anyone have a suggestion about what to do?

  • 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-31T12:06:32+00:00Added an answer on May 31, 2026 at 12:06 pm

    You can parse your file with something like this:

    s = "2 4\n6 8"
    
    s' :: [Int]
    s' = (map read . words) s
    
    let arr = listArray ((1,1),(2,2)) s'
    -- arr == array ((1,1),(2,2)) [((1,1),2),((1,2),4),((2,1),6),((2,2),8)]
    

    By combining words with map read (where read :: (Read a) => String -> a) you get a list [Int].

    So, in order to organize things a little bit, your code in the IO monad may look like this (assuming that every row has the same fixed number of columns and that you take the input file name, the number of rows and of columns as commandline arguments):

    module Main
    
    where
    
    import Data.Array
    import Control.Monad
    import System.Environment
    
    readWords :: (Read a) => String -> [a]
    readWords = map read . words
    
    parseFile :: String -> Int -> Int -> IO (Array (Int, Int) Int)
    parseFile fname rows cols = do
        matr <- liftM readWords $ readFile fname
        return $ listArray ((1, 1), (rows, cols)) matr
        -- (matr :: [Int] is inferred from the parseFile's type)
    
    main :: IO ()
    main = do
        args <- getArgs
        case args of
            [fname, rows, cols] -> do
                arr <- parseFile fname (read rows) (read cols)
                print arr
    

    Note how the conversion function readWords is able to convert to any [a] provided that Read a, so that we don’t restrict ourselves to integers only.
    The liftM function takes a pure function (our readWords) and “lifts” it to work in the current monad, i.e. IO.

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

Sidebar

Related Questions

I am solving problem 9 on the Project Euler . In my solution I
I'm currently working on a project Euler problem (www.projecteuler.net) for fun but have hit
I was solving a Project Euler problem that goes as follows: By considering the
I'm playing with Haskell and Project Euler's 23rd problem. After solving it with lists
Possible Duplicate: Need help solving Project Euler problem 200 Similar to this question Project
I'm working on solving the Project Euler problem 25: What is the first term
I need some help in solving this problem. We have a large amount of
I have the following problem in my Data Structures and Problem Solving using Java
I've been wrestling with Project Euler Problem #16 in C# 2.0. The crux of
I am learning ruby and practicing it by solving problems from Project Euler .

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.