Why would I like to use Maybe instead of List?

Seeing the Maybe type is isomorphic to a multitude of null and singleton lists, why would anyone ever want to use the Maybe type when I could just use the lists to accommodate the absence?

+4
source share
7 answers

Based on the existing (and correct) answers, I mentioned a style based answer.

Different types convey different intentions - a return Maybe ais a computation with the possibility of failure, and [a]may be non-determinism (or, more simply, a lot of possible return values).

, typeclasses - , . Alternative (<|>), , ( ) .

  • Maybe a , , , Nothing
  • [a] , , .

, , , (<|>) -. , , (<|>) - , Haskell: -.

, . , , .

+8

, [] [x], , , , .

Maybe Nothing Just x . , .

, , , , , . "" , , .

+14

. , , . "none" null.

Maybe, . , . "", .

+11

Lisp, Scheme, Python, Ruby, JavaScript .., , Haskell . , JavaScript ( ), , , , .. , - . , , Haskell, , . , ( ) . , .

+4

Maybe. - - .

+3

. , , , , Maybe s, listToMaybe :

listToMaybe :: [a] -> Maybe a
listToMaybe [] = Nothing
listToMaybe (a:_) = Just a

, .

, , , , , , , , . , , - , a -> [b], ( ) . , , , , :

  • - , , :
  • - , , , , .
    • : , , , , , .

Maybe, , none, .

+2

, , QuickCheck - .

0

All Articles