2

I am fairly new to Haskell and trying to improve myself as far as functional languages are concerned. I am attempting to create a function that will take a number of rows and columns and produce a 2D array as follows, for example:

 arrayMaker :: (Int, Int) -> [[Int]]
  -- use replicate function somehow to create each row?

The resulting matrix would appear as follows (for 5 rows, 3 columns):

 [[14, 15, 16],
 [13, 12, 11],
 [8, 9, 10],
 [7, 6, 5],
 [2, 3, 4]]

Notice the values begin in the last row at 2. Also the array should alternate between ascending and descending rows. Help would be greatly appreciated

2
  • Write a function row that given i generates the i-th row. Then map row [1..numRows]. To do that, can you write a simple arithmetical formula for the number in position (i,j) when i is even? and when i is odd? Commented Nov 14, 2014 at 18:00
  • 1
    Note that [Int] in haskell is not an array but a list. In practice you typically would not want to use lists to represent a matrix. Commented Nov 14, 2014 at 18:16

1 Answer 1

3

Let's start with this example:

rowMaker1 n k = [ n .. n+k-1 ] : rowMaker1 (n+k) k

rowMaker1 creates groups of consecutive numbers. It creates an infinite list, so we use take to limit the number of elements computed:

ghci> take 5 (rowMaker1 1 3)
[ [1,2,3], [4,5,6], [7,8,9], [10,11,12], [13,14,15] ]

We can also create a descending version:

rowMaker2 n k = [ n,n-1..n-k+1 ] : rowMaker2 (n-k) k

ghci> take 5 (rowMaker2 16 3)
[[16,15,14],[13,12,11],[10,9,8],[7,6,5],[4,3,2]]

The goal will be to define arrayMaker like this:

arrayMaker (nrows,ncols) = take nrows (rowMaker start ncols)
  where start = ???
        rowMaker n k = ???

We just need a rowMaker which will alternate between ascending and descending groups.

Does this help?

If you get stuck, here's one possibility for the rowMaker function:

rowMaker n k = [ n .. n+k-1 ] : rowMaker2 (n-k) k rowMaker2 n k = [ n, n-1 .. n-k+1 ] : rowMaker (n-k-k+1) k

and you can run this code here: http://tpcg.io/_YJENGK

Sign up to request clarification or add additional context in comments.

6 Comments

sort of. Not sure how to tie rowMaker in to rowMaker1 and rowMaker2
How about having rowMaker create two groups - an ascending group and then a descending group - and then repeat by calling itself.
How would you do this? I am terrible with recursion and that is a large reason of starting Haskell. However, I've been having trouble finding examples pertaining to what I am working with
It will have the form: rowMaker n k = (formula for asc group) : (formula for desc group) : rowMaker new-n k - the colon operator is the basic way to create lists. a:b:c prepends the elements a and b to the front of list c.
@JimWitschey Ok - replaced the lpaste link with a spoiler block
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.