警告:已定义但未使用Haskell

时间:2018-04-12 04:34:49

标签: haskell warnings

我有这个功能,并且正在使用ghc -Wall -Werror来检查警告。从这种方法......

polygonList :: Int -> [Int] -> [Polygon]
polygonList n [] = []
polygonList _ [x] = error "Too few points remaining"
polygonList n (v:y:list') =
    let pointList = take (2*v) list' -- Note: list' may not have 2v points
        points = getPoints pointList
        list'' = drop (2v) list'

        -- Calc Perim Here
        Just (under, over) = calcPerim (fromIntegral y) points :: Maybe (Length, Length)
        poly = Polygon { verticesNum = v, yVal = y, vertices = points, bottom = under, top = over }
        nextPolygon = polygonList (n-1) list''
    in (poly : nextPolygon)

...我得到两个需要修复的警告:

polycake.hs:30:11:警告:已定义但未使用:'ln'

polycake.hs:45:13:警告:已定义但未使用:'n'

功能的详尽模式需要它们,但有没有更好的方法让我不知道?

1 个答案:

答案 0 :(得分:2)

您是否查看了警告指向的代码中的位置?他们为您提供准确的行号和列号。

正如您对问题的评论中所指出的,以下代码中有两个地方有一个已定义但未使用的变量:

polygonList :: Int -> [Int] -> [Polygon]
polygonList n [] = []
--          ^ you define n here but do not use it; use _ instead
polygonList _ [x] = error "Too few points remaining"
--             ^ you define x here but do not use it; use _ instead

应用这些修改后,函数的开头变为:

polygonList :: Int -> [Int] -> [Polygon]
polygonList _ [] = []
polygonList _ [_] = error "Too few points remaining"

下划线_可以在单个模式中多次使用,并表示应忽略该位置的值。这是告诉用户您的模式可能更多地依赖于模式的形状,或仅仅依赖于模式的某些部分的有用方法。

注意:在以下行中,(2v)是错误的,您的意思是(2 * v)吗?

list'' = drop (2v) list'