Em Thinking Functionally with Haskell (p 45), Richard Bird diz que existem seis funções com type Maybe a -> Maybe a
, embora ele cite apenas cinco.
Aplicado à
Nothing
função só pode retornarNothing
ouundefined
. Aplicado àJust x
função só pode retornarNothing
ouJust x
ouundefined
. A questão é que não sabemos absolutamente nada sobre o tipo subjacente, portanto nenhum valor novo pode ser inventado. Isso perfaz seis funções possíveis ao todo.
Eu conto sete:
first, second, third, fourth, fifth, sixth, seventh :: Maybe a -> Maybe a
first (Just x) = Just x
second (Just x) = Nothing
third (Just x) = undefined
fourth Nothing = Nothing
fifth Nothing = undefined
sixth undefined = Nothing
seventh undefined = undefined
Um quebra-cabeça relacionado é que quando tento colocar três dessas definições em uma única função, recebo um erro de compilação "A correspondência de padrão é redundante". Remover qualquer uma das três linhas faz com que ela desapareça
foo :: Maybe a -> Maybe a
foo (Just x) = Nothing
foo Nothing = undefined
foo undefined = Nothing