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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:42:36+00:00 2026-06-02T04:42:36+00:00

Here’s what I came up with: solveRPNWrapper :: (Read a, Integral a) => String

  • 0

Here’s what I came up with:

solveRPNWrapper :: (Read a, Integral a) => String -> a
solveRPNWrapper str = solveRPN [] $ words str

calcFunction :: String -> String -> String -> String
calcFunction "+" x y = show $ read x + read y
calcFunction "-" x y = show $ read x - read y
calcFunction "*" x y = show $ read x * read y
calcFunction "/" x y = show $ read x / read y
calcFunction op x y = error $ "Unknown operator: " ++ op ++ "."

isOperator :: String -> Bool
isOperator "+" = True
isOperator "-" = True
isOperator "*" = True
isOperator "/" = True
isOperator _ = False

solveRPN :: (Read a, Integral a) => [String] -> [String] -> a
solveRPN [] (x:[]) = read x
solveRPN [] (x:y:xs) = solveRPN (x:y:[]) xs
solveRPN stack (x:xs)
         | isOperator x =
           let z = calcFunction x (last (init stack)) (last stack)
           in solveRPN (init (init stack)) (z:xs)
         | otherwise = solveRPN (stack ++ [x]) xs
solveRPN stack [] = error $ "Badly formatted expression: Stack contains " ++ show stack

While it does work …

*Main>  solveRPNWrapper "10 4 3 + 2 *  -"
-4

… I can see this is anything but idiomatic (surely lots of duplication in the operator bit and the read/show seems redundant), and the constraints might be messed up too.

  • 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-02T04:42:38+00:00Added an answer on June 2, 2026 at 4:42 am
    1. Change the type of the stack to Integral a => [a]. This not only removes the need for read and show everywhere, but also reveals a hidden type error in your original code. You were using fractional division (/) instead of integer division (div).

    2. Reverse the stack. Lists are easier to manipulate from the front, so use that as the top of your stack. This also lets us use pattern matching on the stack to pick elements from the top of the stack easily instead of messing around with last and init. This is also more efficient.

    3. Use a lookup table for the operators. This cuts down further on the duplication, and we can just store the corresponding Haskell functions (+, div, etc.) directly in the table.

    This is what I ended up with after making those changes:

    solveRPNWrapper :: (Read a, Integral a) => String -> a
    solveRPNWrapper str = solveRPN [] $ words str
    
    solveRPN :: (Read a, Integral a) => [a] -> [String] -> a
    solveRPN [result] [] = result
    solveRPN (y : x : stack) (token : tokens)
      | Just f <- lookup token operators = solveRPN (f x y : stack) tokens
    solveRPN stack (token : tokens) = solveRPN (read token : stack) tokens
    solveRPN stack [] = error $ "Badly formatted expression: Stack contains " ++ show (reverse stack)
    
    operators :: Integral a => [(String, a -> a -> a)]
    operators = [("+", (+)), ("-", (-)), ("*", (*)), ("/", div)]
    

    You could also use a fold instead of recursion, but that would require adding some more error handling to the wrapper. I’d also consider using just Integer instead of Integral a => a, but that’s just a matter of changing the type signatures.

    For robustness, it would also probably be a good idea to use a pure form of error handling like Either or Maybe instead of using error, and use reads instead of read to handle malformed input.

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

Sidebar

Related Questions

Here is what I am trying to achieve in PHP: I have this string:
Here's my code: string strSQL = SELECT * from tMedia where SKU = '
Here is the code: Function getData(ByVal id As String) Dim reader As SqlClient.SqlDataReader Dim
here's my code private void make_Book(int x, int y, string name) { #region Creating
Here are the 2 methods and the first error is complaining that I need
Here's my proposed (very simplified to illustrate the problem space) design for a C#
Here the total height of all <div> 's are 900 pixels, but the jQuery
Here is a complete example. I want to forbid using A::set from objects casted
Here's the deal: I'm in the process of planning a mid-sized business application that
Here's what I want to do: Given a table PeopleOutfit (id int primary key,

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.