'Detect what function is raising the exception Prelude.!!: negative index
I could not figure out in my code what function is raising (*** Exception: Prelude.!!: negative index since this exception is not very descriptive about the error. Is there any way to detect exactly what custom function is calling !! and raising this error.
This is a simple reproducible example of the error when is called index_around 0 [1, 2, 3], here the exception text doesn't describe if the exception is raised by index_before or by index_after function.
index_before :: Int -> [a] -> a
index_before i list = list !! (i - 1)
index_after :: Int -> [a] -> a
index_after i list = list !! (i + 1)
index_around :: Int -> [a] -> (a, a)
index_around i list = (index_before i list, index_after i list)
Solution 1:[1]
There are a few methods you can use to debug haskell code:
- Using
Debug.Tracewhich logs messages tostderrwherever you use thetracefunction. It requires modification of your code, and sincetraceis an impure function (which hidesIOusingunsafePerformIO), you should use it with caution. - Using the
-xcRTSflag after compiling with-prof -fprof-auto, which gives you the exact call stack when an exception is raised. - Using the
GHCidebugger. A good example of how to use it can be found here.
Solution 2:[2]
When you call index_around 0 [1, 2, 3], you're calling index_before (-1) [1, 2, 3] and index_after 1 [1, 2, 3].
-1 is a negative index.
You may wish to do some bounds checking, and employ the Maybe type. This way the exception never happens.
index_around :: Int -> [a] -> (Maybe a, Maybe a)
index_around n lst
| length lst == 0 = (Nothing, Nothing)
| n == 0 = (Nothing, Just $ index_after n lst)
| n == length lst = (Just $ index_before n lst, Nothing)
| otherwise = (Just ib, Just ia)
where ib = index_before n lst
ia = index_after n lst
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Vikstapolis |
| Solution 2 |
