Turn these haskell Int lists to another

I have the following int lists:

t1 = [1000, 1001, 1002, 1003, 1004] t2 = [2000, 2001, 2002] t3 = [3000, 3001, 3002, 3003] 

The size of the lists of variables, they are not only 3, as in this example. They can have 1 element or many more. Then I have this:

 tAll = [t1, t2, t3] 

I need a function that turns tAll into something like this:

 [[1, 1000, 2000, 3000], [2, 1001, 2001, 3001], [3, 1002, 2002, 3002], [4, 1003, 0, 3003], [5, 1004, 0, 0]] 

Is there an easy way to do this?

EDIT: Sorry, I sent this in a hurry, and that was not quite what I wanted. I updated the code above ...

+4
source share
4 answers

Well, this is Haskell's newbie to write it, but since it does explicit recursion, perhaps the best way. :-)

 head0 [] = 0 head0 xs = head xs tail0 [] = [] tail0 xs = tail xs nreorder n ts | all null ts = [] | otherwise = (n : map head0 ts) : nreorder (n+1) (map tail0 ts) 

And nreorder 1 tAll prints the list you want. You can avoid these indices by doing the following instead:

 reorder ts | all null ts = [] | otherwise = (map head0 ts) : reorder (map tail0 ts) 

so reorder tAll = [[1000,2000,3000],[1001,2001,3001],[1002,2002,3002],[1003,0,3003],[1004,0,0]] and (slightly cleared thanks matiasta):

 nreorder ts = zipWith (:) [1..] (reorder tAll) 

so that nreorder tAll prints the list you want.

+2
source

Here's one liner for you if you're still interested:

 zipWith (:) [1..] $ take (maximum $ map length tAll) (transpose (map (++repeat 0) tAll)) 

edit: OK, it’s better to write it in two lines :)

+9
source

it smells of homework, so I will not give the code, but some tips:
- the length of the resulting lists is the same, which is the maximum number of lists. Find this max and put all other lists with zeros (using laziness, you can fill all lists with an infinite list of zeros).
- get an intermediate version that just prints the first column of your possible answer ...
- when you have this job, get the second column .... and the rest should be doused.

Edit: comments show that this is not homework, therefore:

  list = [[1000, 1001, 1002, 1003, 1004], [2000, 2001, 2002], [3000, 3001, 3002, 3003]]

main = do mapM_ (putStrLn.show) (rotation list) rotate list = [(i + 1): map (!! i) paddedlist | i <- [0 .. (len-1)]] where len = maximum length list $ map paddedlist = map (++ repeat 0) list

+2
source

zip3 will turn 3 lists into one triple list. If you want to use lists of length three instead of them, you can use zipWith3 (\abc -> [a,b,c])

If you need something else in detail (as in your updated request), you will have to minimize it yourself. I would say, first put a termination condition, and then deal with the general case. You should have two helper functions: one for fixing the values ​​as you want (for example, "head_or_zero"), and one for processing input lists that have been completed (for example, "tail_or_nil"):

 fix _ [] [] [] = [] fix i as bs cs = [i, hoz as, hoz bs, hoz cs]:fix (i+1) (ton as) (ton bs) (ton cs) where hoz [] = 0 hoz x:xs = x ton [] = [] ton x:xs = xs 
+1
source

All Articles