为什么我不能定义一个常量而不是在Haskell中的函数中使用它?

时间:2013-04-22 10:48:55

标签: function haskell constants

    element ::Int->Int->[[Int]]-> Int
    element z s m= m!!z!!s
    maxIndex :: Int
    maxIndex = 6  

    matrix :: [[Int]] -> Int -> Int -> Int -> Int
    matrix a row column maxIndex = if maxIndex < length a then error "Out of range"
         else if (row-1<0 || row-1>maxIndex)||(column-1<0 || column-1>maxIndex) then error "Out of range"
         else element (row-1) (column-1) a

我在哪里弄错了?我希望我的程序采用我的maxIndex常量,以便用户不必输入maxIndex的值?

1 个答案:

答案 0 :(得分:1)

maxIndex已经是矩阵词法范围内的已定义标识符。就像元素一样,你不需要做任何特别的事情来使用它,所以你可以说:

matrix :: [[Int]] -> Int -> Int -> Int
matrix a row column = ...