Struggling with lists of lists in Haskell

前端 未结 2 518
失恋的感觉
失恋的感觉 2021-01-28 15:36

Can anybody help me with this function?

 getCell :: [[Int]] -> Int -> Int -> Int  

Where m i j are the indexes of the lines and the c

相关标签:
2条回答
  • 2021-01-28 16:09

    Just for fun - one liner

    getCell l i j = (((l ++ repeat []) !! i) ++ repeat (-1)) !! j 
    
    0 讨论(0)
  • 2021-01-28 16:24

    This should work using the (!!) operator. First check if index is in the list, then access the element at that index using (!!).

    getCell m i j = if i >= length m then -1
                    else let
                             m0 = m !! i
                         in if j >= length m0 then -1
                            else m0 !! j
    
    0 讨论(0)
提交回复
热议问题