George
George

Reputation: 7317

Double wildcarding within a do block in Haskell

Consider the following function which runs a command:

getGrepStdout :: IO String
getGrepStdOut = do let cmd' = "grep"
                       pattern' = "//"
                       options' = "-nR"
                       path' = "/path/to/file"
                       stdin' = ""
                       args' = [pattern', options', path']
                   (_, stdout', _) <- readProcessWithExitCode cmd' args' stdin'
                   return stdout'

Notice the second to last line which has a double _ wild card matching (within a do block). This seems to compile fine in my case, but I was wondering if it was problematic formally speaking?

Upvotes: 1

Views: 79

Answers (3)

rtpg
rtpg

Reputation: 2439

You should be fine (at least with GHC).

It's good to be worried, because if you were to use any other identifier this would blow up!

Prelude> :{
Prelude| do
Prelude|     (x,x) <- return (1,1)
Prelude|     return x
Prelude| :}

<interactive>:12:6:
    Conflicting definitions for ‘x’
    Bound at: <interactive>:12:6
              <interactive>:12:8
    In a pattern binding in
         'do' block

The main thing is that this gets desugared to :

return (1,1) >>= \(x,x) -> return x

And lambda abstractions don't let you use the same local name twice. _ gets special treatment.


So for _, you're telling the compiler that you don't need whatever value's in there. And don't make any assumptions about the value.

In general you shouldn't be worrying about clobbering over your definitions in the same scope, Haskell won't really let you. Here's a related question about not being able to do things like :

plus a a = 2*a
plus a b = a + b

Which is actually pretty similar to this situation. _'s special treatment is needed.

Upvotes: 2

Kwarrtz
Kwarrtz

Reputation: 2753

Strictly speaking, _ isn't really any more of a wildcard pattern than x or arg. What it is, however, is a hole. That means that it can not only accept any value, but won't bind that value to any particular variable. Since _ doesn't bind the value at all, using it twice in the same function declaration is perfectly valid, both theoretically and pragmatically.

Upvotes: 1

obadz
obadz

Reputation: 909

No, there is no problem with having one or more _ in pattern matches, in do-blocks or otherwise. Why do you think there might be?

Upvotes: 4

Related Questions