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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:15:01+00:00 2026-05-30T15:15:01+00:00

I have a Map (Int, Int) a where a can be any type. Based

  • 0

I have a Map (Int, Int) a where a can be any type. Based on the Tuple2 stored in the array as key I wanna create a String table which stores the values of the map.

Because the maximal values of the Tuple2 are known and can be large, my first thought was to create a mutable 2D-Array with a default value. While iterating over the entries of the Map, this array could be filled and then printed out. A pseudo-code example with a Map of Ints:

map = Map (Int, Int) Int
table = Array.ofDim (maxY, maxX) withDefaultValue 0
for ((x,y), int) <- map do
  table[y][x] = int
end
rows = table.rows map toString
str = rows map (toString ++ lineEnd)
print str

The only problem is: I don’t know how to do this in Haskell. Furthermore I don’t know if this is the preferred way one goes in Haskell.

I didn’t find good examples how to use 2D-Arrays in Haskell, thus can someone give me one? If there is a better way to do this, maybe with a built-in data type such as Table, Matrix, 2DStringBuilder etc., can someone show me an example how to use them?

  • 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-30T15:15:03+00:00Added an answer on May 30, 2026 at 3:15 pm
    table = Array.ofDim (maxY, maxX) withDefaultValue 0
    

    That can be done with

    table :: Array (Int,Int) Int
    table = array ((0,0),(maxX,maxY))
                   [((i,j),Map.findWithDefault 0 (i,j) map) | i <- [0 .. maxX], j <- [0 .. maxY]]
    

    Then for tabular output of any two-dimensional array,

    rows :: Array (Int,Int) a -> [[a]]
    rows arr = [[arr ! (r,c) | c <- [clow .. chigh]] | r <- [rlow .. rhigh]]
      where
        ((rlow,clow),(rhigh,chigh)) = bounds arr
    
    rowStrings :: Show a => Array (Int,Int) a -> [String]
    rowStrings arr = [unwords (map show row) | row <- rows arr]
    
    tableString :: Show a => Array (Int,Int) a -> String
    tableString arr = unlines (rowStrings arr)
    
    prettyPrint :: Show a => Array (Int,Int) a -> IO ()
    prettyPrint arr = putStr (tableString arr)
    

    You can also define some the functions point-free,

    prettyPrint = putStr . tableString
    
    tableString = unlines . rowStrings
    
    rowStrings = map (unwords . map show) . rows
    

    rows and table are not comfortably defined point-free, though, so I stop here.

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

Sidebar

Related Questions

If i have a stl map from string to int and i want to
Let's say I have two maps: typedef int Id; std::map<Id, std::string> idToStringMap; std::map<Id, double>
Let's say I have a type of lookup table which I can build for
I have a stl::map which key type is a custom struct. I want to
I have a User table defined like this: CREATE TABLE Users( UserId int IDENTITY(1,1)
I have this struct: struct Map { public int Size; public Map ( int
I have a class like this: class MyClass{ public: MyClass(int Mode); private: std::map <
Supposing to have something like this: #include <map> int main(){ std::map<int,int> m; m[1] =
I have unsorted map of key value pairs. input = { xa => xavalue,
I have a map defined like this std::map<some_key_type, std::string::iterator> mIteratorMap; And a huge string

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.