Morgan O'Brien
Morgan O'Brien

Reputation: 1

Warning: Defined but not used Haskell

I have this function, and am using ghc -Wall -Werror to check for warnings. From this method...

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)

... I get two warnings that I need to fix:

polycake.hs:30:11: Warning: Defined but not used: ‘ln’

polycake.hs:45:13: Warning: Defined but not used: ‘n’

They are needed for the function's exhaustive patterns, but is there a better way that I'm unaware of?

Upvotes: 0

Views: 1550

Answers (1)

Billy Brown
Billy Brown

Reputation: 2342

Did you look at the locations in the code that the warnings point you to? They are giving you exact line and column numbers.

As pointed out in the comments on your question, there are two places in the following code that have a defined but unused variable:

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

With those modifications applied, the beginning of your function becomes:

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

The underscore _ can be used more than once in a single pattern and signifies that the value at that location should be ignored. It is a useful way of telling the user that your pattern might rely more on the shape of your pattern, or only on certain parts of it.

Note: In the following line, (2v) is an error, do you mean (2 * v)?

list'' = drop (2v) list'

Upvotes: 3

Related Questions