Search code examples
haskellidioms

how to have multiple throwaway arguments


I know it's common to have functions like

f _ [] = Nothing
f a (x:xs) = ...

in Haskell it seems idiomatic that _ is the throwaway parameter name.

What if I have lots I don't care about? E.g.

g _ _ [] _     = Nothing
g a _ (x:xs) b = ...

is what I would like to write but I don't think I can have multiple _ in one definition.

So what should I do when I don't care about a lot of them?


Solution

  • You can definitely have more than one underscore in the same function. As they are not assigned to any value, it wouldn't override anything.

    It is also easier to read, as you know it won't matter what the value is and you can pay attention to what actually contributes to the result. Also, If you enable all the warnings when compiling, you will actually get an error if you do not use an argument that has a name:

    g a b = b
    

    will return a Warning: Defined but not used: 'a'