在Haskell中与列表进行斗争 [英] Struggling with lists of lists in Haskell

查看:105
本文介绍了在Haskell中与列表进行斗争的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

其中m i j是列表m列表的行和列的索引。
索引从零开始,每行都是相同的大小。
如果我或j无效,函数应该返回-1。



我正在Haskell上进行考试,尽管这可能会显示我仍然想知道我该怎么做,因为我从来没有在Haskell中使用过列表,我不知道如何开始解决这个问题。你可以给我一个手吗?



这是我迄今为止所做的:

  getCell :: [[Int]]  - > Int  - > Int  - > Int 
getCell [] _ _ =列表为空!
getCell zs x y =
if x>长度zs || y> length(z:zs)then -1 else
let row = [x | x == !!头z < - zs]
列= ...

我不知道如何查找行和列

解决方案

这应该使用(!!)运算符。首先检查索引是否在列表中,然后使用(!!)访问该索引处的元素。

  getCell mij = if i> = length m then -1 
else let
m0 = m !! i
in if j> = length m0 then -1
else m0 !! j


Can anybody help me with this function?

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

Where m i j are the indexes of the lines and the columns of the list of lists m. the indexes start from zero and every line is the same size. The function should return -1 if i or j are not valid.

I'm having an exam on Haskell, and despite the fact that this might show up, i still want to know how can i do it, and because i've never worked with lists of lists in Haskell, i have no idea how to start solving this problem. Can you give me a hand ?

here's what i've done so far:

getCell :: [[Int]] -> Int -> Int -> Int
getCell [] _ _ = "the list is empty!"
getCell zs x y =
    if x > length zs || y > length (z:zs)  then -1 else
        let row = [x| x == !! head z <- zs]
            column = ...

I don't know how to find the rows and the columns

解决方案

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

这篇关于在Haskell中与列表进行斗争的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆