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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:24:55+00:00 2026-05-14T14:24:55+00:00

Let me establish that this is part of a class assignment, so I’m definitely

  • 0

Let me establish that this is part of a class assignment, so I’m definitely not looking for a complete code answer. Essentially we need to write a converter in Scheme that takes a list representing a mathematical equation in infix format and then output a list with the equation in postfix format.

We’ve been provided with the algorithm to do so, simple enough. The issue is that there is a restriction against using any of the available imperative language features. I can’t figure out how to do this in a purely functional manner. This is our fist introduction to functional programming in my program.

I know I’m going to be using recursion to iterate over the list of items in the infix expression like such.

(define (itp ifExpr)
  (
    ; do some processing using cond statement
    (itp (cdr ifExpr))
  ))

I have all of the processing implemented (at least as best I can without knowing how to do the rest) but the algorithm I’m using to implement this requires that operators be pushed onto a stack and used later. My question is how do I implement a stack in this function that is available to all of the recursive calls as well?

  • 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-14T14:24:55+00:00Added an answer on May 14, 2026 at 2:24 pm

    (Updated in response to the OP’s comment; see the new section below the original answer.)


    Use a list for the stack and make it one of the loop variables. E.g.

    (let loop ((stack (list))
               ... ; other loop variables here,
                   ; like e.g. what remains of the infix expression
                )
      ... ; loop body
      )
    

    Then whenever you want to change what’s on the stack at the next iteration, well, basically just do so.

    (loop (cons 'foo stack) ...)
    

    Also note that if you need to make a bunch of “updates” in sequence, you can often model that with a let* form. This doesn’t really work with vectors in Scheme (though it does work with Clojure’s persistent vectors, if you care to look into them), but it does with scalar values and lists, as well as SRFI 40/41 streams.


    In response to your comment about loops being ruled out as an “imperative” feature:

    (let loop ((foo foo-val)
               (bar bar-val))
      (do-stuff))
    

    is syntactic sugar for

    (letrec ((loop (lambda (foo bar) (do-stuff))))
      (loop foo-val bar-val))
    

    letrec then expands to a form of let which is likely to use something equivalent to a set! or local define internally, but is considered perfectly functional. You are free to use some other symbol in place of loop, by the way. Also, this kind of let is called ‘named let‘ (or sometimes ‘tagged’).

    You will likely remember that the basic form of let:

    (let ((foo foo-val)
          (bar bar-val))
      (do-stuff))
    

    is also syntactic sugar over a clever use of lambda:

    ((lambda (foo bar) (do-stuff)) foo-val bar-val)
    

    so it all boils down to procedure application, as is usual in Scheme.

    Named let makes self-recursion prettier, that’s all; and as I’m sure you already know, (self-) recursion with tail calls is the way to go when modelling iterative computational processes in a functional way.

    Clearly this particular “loopy” construct lends itself pretty well to imperative programming too — just use set! or data structure mutators in the loop’s body if that’s what you want to do — but if you stay away from destructive function calls, there’s nothing inherently imperative about looping through recursion or the tagged let itself at all. In fact, looping through recursion is one of the most basic techniques in functional programming and the whole point of this kind of homework would have to be teaching precisely that… 🙂

    If you really feel uncertain about whether it’s ok to use it (or whether it will be clear enough that you understand the pattern involved if you just use a named let), then you could just desugar it as explained above (possibly using a local define rather than letrec).

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

Sidebar

Related Questions

First let's establish this. I have public abstract class Foo { public static void
First, let's say I don't want to use duplex channel and this is not
Let's consider the following scenario: a function which can generate code colors from white
Let's say i have two tables in db: Car and Part. Car owns arbitrialy
Let me start by telling you that I never used anything besides SVN and
Let's assume that I am at manual A. I press enter and I am
Let’s say I have a number like 0x448 . In binary this is 0100
Let's say I have a structure named vertex with a method that adds two
Let me state up front that I have an infantile understanding of Monads. I
I'm a Linq beginner so just looking for someone to let me know if

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.