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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:39:53+00:00 2026-05-26T09:39:53+00:00

I want to develop a sort of stack managing system. The list starts by

  • 0

I want to develop a sort of stack managing system. The list starts by being empty [] and a user can input numbers and they will be added to the list, as well as binary operations, which will take two first numbers from a list and perform the operation and then put it back on the list. EG:

[] : 3
[3] : 4
[4,3] : +
[7] : c
[] : 123
[123] : 3
[3,123] : *
[369] :

I cannot figure out how to process input from the console. I have this broken code:

import System.Environment   
import System.Directory  
import System.IO  
import Data.List  

stack = []

add1 :: [Int] -> [Int]
add1 [] = []
add1 [x] = [x]
add1 [x,y] = [(x+y)]
add1 x:(y:xs) = (x+y) : (xs : []) 

--sub :: [Int] -> [Int]
--sub [] = []
--sub x:(y:xs) = (x-y) : xs 

--mul :: [Int] -> [Int]
--mul [] = []
--mul x:(y:xs) = (x*y) : xs 

--div :: [Int] -> [Int]
--div [] = []
--div x:(y:xs) = (x/y) : xs 

c :: [Int] -> [Int]
c = []

push :: [Int] -> a -> [Int]
push [] a = [a]
push (x:xs) a = a : (x:xs)

dispatch :: [(String, Int -> IO ())]
dispatch =  [ ("+", add)
       --     , ("-", sub)
       --     , ("*", mul)
       --     , ("/", div)
            ]

xcl = do
    print stack
    answer <- readLine

But I don’t even know if I’m heading in the right direction. Any help will be great. Thank you.

  • 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-26T09:39:54+00:00Added an answer on May 26, 2026 at 9:39 am

    You are on the right path; lets take a look at some changes to your code I would suggest.

    Your add1 function is quite close, but you’ve got two small problems – the first is the pattern match you provide: to be syntactically correct, you need the whole match to be inside the parentheses. The second is the second cons (colon). cons has a type of a -> [a] -> [a], so it works perfectly with (x+y) as the first parameter; however, since xs has type [Int] already, you don’t need to supply : [].

    add1 (x:y:xs) = (x+y) : xs
    

    Next, because you are dealing entirely with Ints and lists of Ints, using some type a does not make sense in this context.

    push :: [Int] -> Int -> [Int]
    

    Finally, your workhorse function. Because of the lack of loop constructs in Haskell, the way to do a user input loop (also known an REPL) is through recursion. Because of this, it would make some sense to accept a parameter. Let’s make that your [Int] stack. The function to read one line from stdin as a string is getLine, which has type IO String. Finally, you actually have to handle that input. For simplicity, I’ve just included that logic in a case statement in xcl, but it could as well have been done using dispatch or a similar function (In fact, if your RPN calculater becomes more complicated, this would have its merits). The action for each case should recurse into your xcl “loop” with a modified stack. When you quit, it should just exit – which is a good use of return.

    xcl :: [Int] -> IO ()
    xcl st = do
        print st
        answer <- getLine
        case answer of
          "q" -> return ()
          "c" -> xcl ([] ::[Int])
          "+" -> xcl $ add1 st
          x -> xcl $ push st $ read x
    

    In fact, you could take this one step further and protect against exceptions – what will happen when the user passes in some non-function, non-number string? The code above will fail, with a “no parse” exception. The best way around that is to use reads in place of read, as is discussed in this answer. reads returns either a list with a single entry – a tuple containing a parsed number and a remaining string, or an empty list (indicating a failed read). For example:

          x -> case (reads x) of
            [(num,_)] -> xcl $ push st num
            _ -> xcl st
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to develop simple Serverless LAN Chat program just for fun. How can
I want to develop a mobile web application using asp.net 3.5 that can be
I need to develop a client server system where I can have multiple clients
I want to develop a system that is similar to calculation of salary. A
I want to develop a Ip messenger app in VB.Net for all the user
I want to develop a windows application. If I use native C++ and MFC
I want to develop Java apps, real quick, what IDE should I choose?
I want to develop a very simple 2D game in Python. Pygame is the
I want to develop an iPhone application using the utility template, where the flip
I want to develop ASP.NET C# based MMOG (Massively multiplayer online game). I would

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.