3

Being a little bit frustrated over the error messages some prelude functions (like !!) give, I tried to write a different version.

--(!!!) :: (Show a,Integral b)=> [a]->b->a
as !!! y=f as y
    where f (x:xs) b= if b==0
                      then x
                      else f xs (b-1)
          f [] _= error "!!!: list "++(show as)++" has less than "++show y++" elements"

However the function type is

*Handydandy> :type (!!!)
(!!!) :: (Show a, Num a, Eq a) => [[Char]] -> a -> [Char]

I do not understand why the first argument is inferred to be a list of Strings here instead of just a list of show instances. Could anybody explain?

3
  • Note that your f helper isn't really useful—(!!!) can just call itself recursively.
    – dfeuer
    Jan 27, 2015 at 14:28
  • 3
    @dfeuer It wouldn't actually be equivalent, the way he has it defined means that if the index is out of bounds the error message will contain the original list and index passed to !!!, which isn't possible (to my knowledge) without a helper function.
    – bheklilr
    Jan 27, 2015 at 14:31
  • @bheklilr, ah yes, I didn't notice that. I don't usually pay too much attention to the contents of error messages! I should note that doing things like this can have a performance impact as it needs to hold on to a pointer to the front of the list until it completes successfully.
    – dfeuer
    Jan 27, 2015 at 18:05

2 Answers 2

8

You need some parenthesis (or a $) in the last line:

error ("!!!: list "++(show as)++" has less than "++show y++" elements")

Right now its being parsed as

(error "!!!: list ") ++ (show as) ++ " has less than " ++ show y ++" elements"

which makes Haskell think that (!!!) is returning a string, which then would mean that its input must be a list of strings.

6

It's because of your use of error. The type of error is String -> a, and the way you've typed it the compiler sees it as

f [] _ = (error "!!!: list ") ++ (show as) ++ " has less than " ++ (show y) ++ " elements"

Since you have a value of type a concatenated with a String (i.e. error "!!!: list " ++ show as), then a must be String, and so the return type of f must be String. You have f (x:xs) 0 = x, so x must have type String, and therefore xs :: [String].

You can fix this with $:

f [] _ = error $ "!!!: list " ++ show as ++ " has less than " ++ show y ++ " elements"

And now the type of this function is (!!!) :: (Show t, Show a, Num a, Eq a) => [t] -> a -> t, more like what you want.

However, if you want a better way to handle this in code instead of just in a runtime error message you can use the Maybe data type, or even the Either type if you want more information:

(!!?) :: [a] -> Int -> Maybe a
[]     !!? _ = Nothing
(x:_)  !!? 0 = Just x
(_:xs) !!? n = xs !!? (n - 1)

(!!^?) :: [a] -> Int -> Either ([a], Int) a
ys !!^? m = go ys m
    where
        go []     _ = Left (ys, m)
        go (x:_)  0 = Right x
        go (_:xs) n = go xs $ n - 1

Then you could have

showIndexError :: Show a => Either ([a], Int) a -> String
showIndexError (Left (xs, n)) = "!!^?: list " ++ show xs ++ " has less than " ++ show y ++ " elements"
showIndexError (Right x) = show x
1
  • thanks. I'll probably graduate to maybe and its ilk some day.
    – timwaagh
    Jan 27, 2015 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.