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

  • SEARCH
  • Home
  • 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 7697583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:03:03+00:00 2026-05-31T22:03:03+00:00

So I have this calculator I’m building that accepts user variable inputs like let

  • 0

So I have this calculator I’m building that accepts user variable inputs like “let a = 2” These variable are stored in a List of Tuples (Variable, value) I need help with getting the data from this list. My code so far

primary :: Parser Float
primary = do symbol "("
         e <- expression
         symbol ")"
         return e
       +++ do v <- identifier                 
              let a = (find (==(head v)) vlist)
              return a

I get an error because find returns a Maybe and I need it to return a Float or give the user an error message. How do I do this?

  • 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-31T22:03:05+00:00Added an answer on May 31, 2026 at 10:03 pm

    I’m not sure where vlist comes from. It should probably be part of the parser’s user state. For now, let’s assume it’s a top-level definition:

    vlist :: [(String, Float)]
    vlist = undefined -- fill in the blanks...
    

    I assume you’re using Parsec. You can simplify your parser to:

    primary :: Parser Float
    primary = choice [ between (symbol "(") (symbol ")") expression
                     , do { ident <- identifier
                          ; case lookup ident vlist of
                              Nothing -> fail $ "No such identifier: " ++ ident
                              Just v -> return v
                          }
                     ]
    

    You have several options for how to deal with the error. Here, I have used the parser monad’s fail function. This will cause the parser to return a Left parserError. Alternatively, you could substitute error for fail, which will result in an error that can only be handled in the IO monad.

    Note: To add the vlist as parser state, you need to define a new parser type with that state:

    data MyParserState = MyParserState { vlist :: [(String, Float)] }
    type MyParser = CharParser MyParserState
    
    -- these parsers now need to return MyParser type!
    symbol :: String -> MyParser String
    identifier :: MyParser String
    expression :: MyParser Float
    
    primary :: MyParser Float
    primary = choice [ between (symbol "(") (symbol ")") expression
                     , do { st <- getState
                          ; ident <- identifier
                          ; case lookup ident $ vlist st of
                              Nothing -> fail $ "No such identifier: " ++ ident
                              Just v -> return v
                          }
                     ]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a WPF calculator app. In this application I have TextBox that get
I have a currency calculator that requires buttons to convert to/from currencies. I'd like
So i have this code that creates a Calculator in Java. But it is
I have a simple calculator that works, but my problem is if the user
I'm trying to make a calculator which is basicly like this: user enters about
Situation: I have a calculator that I'm developing using these formulas: // --- Math
I have this routine that calculates the seconds-to-date for a struct tm . On
I have this function that takes the input of a, runs a calculation and
I have a calculated field in a list with this formula: =CID & -
I have a form/calculator, which posts to itself some data, this data is then

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.