Haskell: Expected Laziness, Why is this Evaluated?
-
12-11-2019 - |
题
I have a function sideH
which runs the risk of Prelude.head []
. Hence, I have written it using Maybe, to avoid this:
sideH :: Residue -> Maybe (Atom, Atom)
sideH res
-- Make sure the elements exist
| nits /= [] && cars /= [] && oxys /= [] = Just (newH1, newH2)
| otherwise = Nothing where
...
The above works exactly as expected, without error. Now, in the function which calls sideH
(which is not a do construct), I must handle the situation that sideH
returns Nothing
:
callerFunc :: [Residue] -> Aromatic -> [(Double, Double)]
callerFunc [] _ = []
callerFunc (r:rs) aro
-- Evaluate only if there is something to evaluate
| newHs /= Nothing = (newH1Pos, newH2Pos)
| otherwise = callerFunc rs aro where
newHs = sideH r
newH1Pos = atomPos $ fst $ fromJust newHs
newH2Pos = atomPos $ snd $ fromJust newHs
If I try to evaluate newH1Pos
or newH2Pos
when newH = Nothing
, it will fail because fromJust Nothing
is an error. However, I expect this to never happen. I expect the callerFunc
to evaluate newHs
, which is either Just something
or Nothing
. If it is Nothing
, then the callerFunc
will go to its next step without ever evaluating newH1Pos
or newH2Pos
. This does not appear to be the case. I get an *** Exception: Maybe.fromJust: Nothing
error exactly where I would expect newHs
to return Nothing
.
I was asked for more code. I am trying to come up with a minimum situation that reproduces the error, but in the mean time, here is the complete problematic callerFunc
code.
-- Given a list of residues and an aromatic, find instances where there
-- is a Hydrogen bond between the aromatic and the Hydrogens on Gln or Asn
callerFunc :: [Residue] -> Aromatic -> [(Double, Double)]
callerFunc [] _ = []
callerFunc (r:rs) aro
-- GLN or ASN case
| fst delR <= 7.0 && (resName r == gln || resName r == asn) &&
newHs /= Nothing && snd delR <= 6.0 =
[(snd delR, fst delR)] ++ hBondSFinder rs aro
| otherwise = hBondSFinder rs aro where
-- Sidechain identifying strings
gln = B.pack [71, 76, 78]
asn = B.pack [65, 83, 78]
-- Get the location of the Hydrogens on the residue's sidechain
newHs = sideH r
newH1Pos = atomPos $ fst $ fromJust newHs
newH2Pos = atomPos $ snd $ fromJust newHs
-- Get the location of the Nitrogen on the mainchain of the Residue
ats = resAtoms r
backboneNPos = atomPos $ head $ getAtomName ats "N"
hNVect1 = Line2P {lp1 = newH1Pos, lp2 = backboneNPos}
hNVect2 = Line2P {lp1 = newH2Pos, lp2 = backboneNPos}
interPoint1 = linePlaneInter (aroPlane aro) hNVect1
interPoint2 = linePlaneInter (aroPlane aro) hNVect2
delR = minimum [(interPoint1 `dist` newH1Pos, delr1),
(interPoint2 `dist` newH2Pos, delr2)]
delr1 = interPoint1 `dist` (aroCenter aro)
delr2 = interPoint2 `dist` (aroCenter aro)
I know this is a painful code dump. I am trying to whittle it down.
没有正确的解决方案