Usando Tal vez escriba en Haskell
Estoy tratando de utilizar el tipo Maybe en Haskell. Tengo una búsqueda de clave, valor tuplas que devuelve un Tal vez. ¿Cómo puedo acceder a los datos que fueron envueltos por Maybe? Por ejemplo, quiero agregar el entero contenido por Tal vez con otro entero.
6 answers
Alternativamente puede coincidir con el patrón:
case maybeValue of
Just value -> ...
Nothing -> ...
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-09-04 18:09:16
Puedes usar Data.Maybe.fromMaybe
, que toma un Maybe a
y un valor para usar si es Nothing
. Podría usar el no seguro Data.Maybe.fromJust
, que simplemente se bloqueará si el valor es Nothing
. Es probable que desee mantener las cosas en Maybe
. Si desea agregar un entero en un Maybe
, podría hacer algo como
f x = (+x) <$> Just 4
Que es lo mismo que
f x = fmap (+x) (Just 4)
f 3
será entonces Just 7
. (Puede continuar encadenando cálculos adicionales de esta manera.)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-12-05 05:52:29
Como nota al margen: Dado que Maybe
es un Monad
, puede construir cálculos usando do
-notation ...
sumOfThree :: Maybe Int
sumOfThree = do
a <- someMaybeNumber
b <- someMaybeNumber
c <- someMaybeNumber
let k = 42 -- Just for fun
return (a + b + c + k)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-09-05 00:08:14
Ejemplos para "tal vez":
> maybe 0 (+ 42) Nothing
0
> maybe 0 (+ 42) (Just 12)
54
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-09-06 09:25:31
Muchas personas están en contra del uso de fromJust
, sin embargo, puede ser conveniente si usted es consciente de lo que sucederá cuando la búsqueda falla (¡error!!)
Primero necesitarás esto:
import Data.Maybe
Y luego su búsqueda de una lista de tuplas se verá así
Data.Maybe.fromJust $ lookup key listOfTuples
Por ejemplo, búsqueda exitosa:
Data.Maybe.fromJust $ lookup "a" [("a",1),("b",2),("c",3)]
1
Y el horrible fracaso se ve así:
Data.Maybe.fromJust $ lookup "z" [("a",1),("b",2),("c",3)]
*** Exception: Maybe.fromJust: Nothing
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-09-24 10:14:06
Lo siento, debería haber buscado en Google mejor.
Usar la función fromMaybe es exactamente lo que necesito. fromMaybe devolverá el valor en Maybe si no es nada, de lo contrario devolverá un valor predeterminado suministrado a fromMaybe.
Http://www.haskell.org/ghc/docs/6.12.2/html/libraries/base-4.2.0.1/Data-Maybe.html
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-09-04 16:57:07