This is going to be hard to explain because there is a decent amount of background detail about the code as a whole that needs to be known to really know functionally what I'm talking about. But I'll try my best to just get my main point across, and hope that it's enough. Let me know if not and I'll add more information. So:
I have a Haskell function eval :: WExp -> Memory -> WValue
with a bunch of different instances of itself for different cases. For now, knowledge about WExp
, Memory
, and WValue
is not relevant. My problem is that, for a specific instance of eval
, I am using a lookup
function, which takes the parameter of eval
(a string in this case) searches a list of key-value pairs for that string. Note that this lookup
function is not the one included in the Prelude; it is self-defined within the .hs file. If the string is found, the value associated with it is returned, but if it is not found, Nothing
is returned. Because of the Nothing
case, the type of lookup
is actually Maybe a
, where a
would be a WValue
in this case. Because eval
would return a Maybe WValue
, the compiler obviously complains that the type is not WValue
.
Again, if you need more information on what these other types are, I can provide it. It is just my thought that there might be some kind of general method to extract the a
value from any function that returns Maybe a
. If not, I guess I'll look elsewhere for solutions :)
If you know that the lookup is successful, and that the
Maybe a
is actuallyJust a
, you can simply pattern match:and there you have your
val::a
out of yourMaybe a
. Note that this is unsafe code which will ungracefully throw an error iflookup
returns aNothing
.The function you are looking for is
maybe
defined in Prelude.You need to decide on what to return if the expression is Nothing. Lets say you want to get empty string
""
for Nothing. Then the following will let you get out of Maybe boxes.Do this
Don't do this
This is bad because your program just goes splat if the user input is wrong.
Really don't do this
There is a function called
fromJust
that attempts to pull a value out of aMaybe
and throws an error if it findsNothing
. It looks likeThis makes it hard to see what went wrong.
And really, really don't do this
But if you want to play with fire, you can try it just for fun. This will attempt to get a value out of a
Maybe
and crash real hard if it findsNothing
. By "crash real hard" I mean you'll get a segmentation fault if you're lucky, and you'll publish your private keys on the web if you're not.Well, you got yourself into a quagmire because the type of your
lookup
says that it could fail. Haskell forces you in this case to deal with the possibility that such a failure will occur. This is the case iflookup
returnsNothing
.If you are really sure that
lookup
never fails (maybe because you preprocessed and type-checked the program, or you really trust it :) ) you could usefromJust
fromData.Maybe
. Note that is is really just a band-aid solution becausefromJust
will produce a (Haskell) runtime error on its own if called withNothing
.