9

I know I can usually just pattern match, but sometimes I would find these functions useful:

isLeft  = either (const True) (const False)
isRight = either (const False) (const True)

Is there something like that in the standard library?

fredoverflow
  • 256,549
  • 94
  • 388
  • 662

5 Answers5

27

While this is pretty old, posting here for reference.

This is now in the standard library under Data.Either since 4.7:

https://hackage.haskell.org/package/base-4.7.0.0/docs/Data-Either.html

isLeft :: Either a b -> Bool

Return True if the given value is a Left-value, False otherwise.

isRight :: Either a b -> Bool

Return True if the given value is a Right-value, False otherwise.

pyrospade
  • 7,870
  • 4
  • 36
  • 52
14

As people have been pointing out, there is no such function in the standard library, and you can implement your own in various ways.

However, note that questions of the form "Is X in the standard library?" are most easily answered by Hoogle, since even if you don't know the name of a function, you can search for it by its type.

Hoogle is also smart enough to know that the argument order does not matter, and it will also show results whose types are similar (e.g. more generic) than the type you searched for.

In this case, searching for Either a b -> Bool does not yield any promising matches, so that's a good indicator that it does not exist in the standard library.

hammar
  • 138,522
  • 17
  • 304
  • 385
  • 1
    The advice for Hoogle is still good, but note that the standard library *does* have such a function now; see [the other answer to this question](http://stackoverflow.com/a/24046158/1126841). – chepner Apr 12 '16 at 14:02
5

No, but you can write:

import Data.Either

isLeft = null . rights . return
isRight = null . lefts . return
Landei
  • 54,104
  • 13
  • 100
  • 195
  • 2
    Probably because it's a rather non-straightforward way of defining the functions. A definition in terms of `either` or using pattern matching would probably be more understandable. – Erik Hesselink Aug 28 '11 at 19:40
  • Could be. I'm a little bit disappointed how few functions are available for `Either`, there is not much to work with. I would at least expect something like `leftMaybe :: Either a b -> Maybe a` and its counterpart. – Landei Aug 29 '11 at 06:23
3

No, there isn't, afaik.

But you can define these functions even easier*:

isLeft (Left _) = True
isLeft _        = False

the same goes for isRight, of course.

EDIT: * Okay, I guess it's arguable if that's easier or not, since it requires more lines of code...

bzn
  • 2,362
  • 1
  • 17
  • 20
2

As far as I know, there's nothing like this in the standard library. You can define it yourself easily, however.

either l _ (Left  a) = l a
either _ r (Right b) = r b

isLeft (Left _) = True
isLeft _        = False

isRight (Right _) = True
isRight _         = False
Anon
  • 31
  • 1
  • 2
    The OP wants to know whether `isLeft` and `isRight` are in the standard library under different names. He knows he can already define them in terms of `either`, which *is* in the standard library. – dave4420 Aug 27 '11 at 09:39