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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:35:14+00:00 2026-05-29T20:35:14+00:00

Just started with Scheme. I’m having problem with printing on console. A simple list

  • 0

Just started with Scheme. I’m having problem with printing on console.
A simple list printing example:

 (define factorial
   (lambda (n)
     (cond 
       ((= 0 n) 1)
       (#t (* n (factorial (- n 1)))))))

I want to print n, every time the function is called. I figured that I can’t do that within the same function? Do I need to call another function just so I can print?

  • 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-29T20:35:15+00:00Added an answer on May 29, 2026 at 8:35 pm

    Printing in Scheme works by calling display (and possibly, newline).
    Since you want to call it sequentially before/after something else (which, in a functional (or in the case of Scheme, functional-ish) language only makes sense for the called functions side-effects), you would normally need to use begin, which evaluates its arguments in turn and then returns the value of the last subexpression. However, lambda implicitly contains such a begin-expression.

    So in your case, it would go like this:

     (lambda (n)
       (display n) (newline)
       (cond [...]))
    

    Two remarks:

    1. You can use (define (factorial n) [...]) as a shorthand for (define factorial (lambda (n) [...])).
    2. The way you implement factorial forbids tail call-optimization, therefore the program will use quite a bit of stack space for larger values of n. Rewriting it into a optimizable form using an accumulator is possible, though.

    If you only want to print n once, when the user calls the function, you will indeed need to write a wrapper, like this:

     (define (factorial n)
       (display n) (newline)
       (inner-factorial n))
    

    And then rename your function to inner-factorial.

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

Sidebar

Related Questions

Just started working with .NET and MVC(1). I'm having a problem wherein in my
I'm just getting started writing a simple web crawler to get info on links
I'm just started learning scheme and can't quite understand why this function does not
Having just started working for a new client I have noticed that their database
Just started learning Scheme. I'm using Dr. Racket as my compiler/interpreter. I need some
Just started getting a bunch of errors on our C# .Net app that seemed
Just started to get my feet wet with C# and .NET, liking it so
Just started with JQuery and I've managed to pass a parameter using POST, firebug
Just started my first MVC 2.0 .net application. And I set up some default
Just started coding in AS3 with FlashDevelop and coming from a C# background, I

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.