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 7864261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:43:53+00:00 2026-06-02T23:43:53+00:00

This portion of code should read in two or more numbers (main io function

  • 0

This portion of code should read in two or more numbers (main io function omitted), then a “+” to give the sum. Rationals are used because later i will do multiplications and such other operations.

data Expression =  Number Rational
               | Add (Expression)(Expression)
               deriving(Show,Eq)

solve :: Expression -> Expression
solve (Add (Number x) (Number y)) = Number (x + y)

parse :: [String] -> [Expression] -> Double
parse ("+":s)(a:b:xs) = parse s (solve (Add a b):xs)
parse [] (answer:xs) = fromRational (toRational (read (show answer)::Float))
parse (x:xs) (y) = parse (xs) ((Number (toRational (read x::Float))):y)

The (second) error is with the parse function unable to handle

*Main> parse ["1","2","+"] [Number 3]

*** Exception: Prelude.read: no parse

I have looked on the Data.Ratio page and on the web for this solution but haven’t found it and would appreciate some help. Thanks,

CSJC

  • 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-06-02T23:43:55+00:00Added an answer on June 2, 2026 at 11:43 pm

    The first equation,

    parse ("+":s)(a:b:xs) = parse (s)((solve (Add (Number a) (Number b))):xs)
    

    should be

    parse ("+":s)(a:b:xs) = parse (s)((solve (Add a b)):xs)
    

    since per the type signature, a and b already are Expressions.

    Or, in line with the second and third equations, change the type to

    parse :: [String] -> [Rational] -> Double
    

    and change the first equation to

    parse ("+":s)(a:b:xs) = parse s ((a + b):xs)
    

    Two possible ways of fixing the code (there were more problematic parts):

    -- Complete solve to handle all cases
    solve :: Expression -> Expression
    solve expr@(Number _) = expr
    solve (Add (Number x) (Number y)) = Number (x + y)
    solve (Add x y) = solve (Add (solve x) (solve y))
    
    -- Convert an Expression to Double
    toDouble :: Expression -> Double
    toDouble (Number x) = fromRational x
    toDouble e = toDouble (solve e)
    
    -- parse using a stack of `Expression`s
    parse :: [String] -> [Expression] -> Double
    parse ("+":s) (a:b:xs) = parse s ((solve (Add a b)):xs)
    parse [] (answer:_) = toDouble answer
    parse (x:xs) ys = parse xs (Number (toRational (read x :: Double)) : ys)
    parse _ _ = 0
    
    -- parse using a stack of `Rational`s
    parseR :: [String] -> [Rational] -> Double
    parseR ("+":s) (a:b:xs) = parseR s (a+b : xs)
    parseR [] (answer:xs) = fromRational answer
    parseR (x:xs) y = parseR xs ((toRational (read x::Double)):y)
    parseR _ _ = 0
    

    The latter is rather circumspect, since in the end a Double is produced, there’s no real point using Rationals for the stack.

    In your code for parse, the third equation leaves out the conversion of a Rational to an Expression via the Number constructor, but is otherwise fine. The second equation, however, contains a different type of problem:

    parse [] (answer:xs) = fromRational (toRational (read (show answer)::Float))
    

    If answer is either an Expression or a Rational, show answer cannot be parsed as a Float, so that will lead to a runtime error, as exemplified by your edit:

    The (second) error is with the parse function unable to handle

    *Main> parse ["1","2","+"] [Number 3]
    *** Exception: Prelude.read: no parse
    

    At the point where the second equation is used, the first element (answer) on the stack is Number (3 % 1), and show (Number (3 % 1)) is "Number (3 % 1)", which is not a String that read can parse as a Float.

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

Sidebar

Related Questions

following up from yesterday... This portion of the code does work. $(document).ready(function(){ $('#listMenu a').click(function
This portion of code checks if a number given (First element in a list)
Could someone explain to me what this portion of code means? repeat scroll 0
With ASP .NET MVC3. In my controller I have this portion of code MasterMindDnetEntities
I am trying to make a simple alarm. in this code portion a take
Let's say we have a code portion like this: IProduct product = ProductCreator.CreateProduct(); //Factory
I'm looking through some code for learning purposes. I'm working through this portion of
i don't understand how this happen. This is portion of my code.. int isGoal(Node
This is super strange. I used a portion of code for a new project
I needed some help in trying to optimize this code portion ... Basically here's

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.