Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7551877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:36:44+00:00 2026-05-30T10:36:44+00:00

I have a function sideH which runs the risk of Prelude.head [] . Hence,

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T10:36:45+00:00Added an answer on May 30, 2026 at 10:36 am

    The answer to this question (asked in the comments) doesn’t fit in a comment: “I am not sure how I would use pattern matching, here, to remove these if statement.”.

    Like this, for example, though there are still some code smells left that could likely be improved with some additional refactoring:

    sideH :: Residue -> Maybe (Atom, Atom)
    sideH res = case (nits, cars, oxys) of
        (_:_, _:_, _:_) -> Just (newH1, newH2)
        _ -> Nothing
        where
        ...
    

    If you have flexible morals, you might try something like this:

    sideH :: Residue -> Maybe (Atom, Atom)
    sideH res = do
        _:_ <- return nits
        _:_ <- return cars
        _:_ <- return oxys
        return (newH1, newH2)
        where
        ...
    

    Again, both of these code samples can likely be improved about tenfold if there’s a bit more context and code available to make recommendations for.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have written following function in JS which is called on click of some
I have a function which returns a one-sided intersection of values between two input
I have function some_func_1 which will create an object of type some_type and will
I have function like this: function gi_insert() { if(!IS_AJAX){ $this->load->library('form_validation'); $this->form_validation->set_rules('name', 'Naslov', 'trim|required|strip_tags'); $this->form_validation->set_rules('body',
I have an onclick function which performs several tasks. In another JavaScript function I
I have a function which returns non-void. On the caller side, I want to
i have a code which is like this : private void testToolStripMenuItem_Click(object sender, EventArgs
I have a function that which process the side bar of a web page
I have a function that is side-effect free. I would like to run it
I have an object Dictionary<int, Dictionary<int, Foo> > on one side, and a function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.