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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:07:28+00:00 2026-05-15T19:07:28+00:00

I’ve been learning scheme, and I just realized that I don’t really know how

  • 0

I’ve been learning scheme, and I just realized that I don’t really know how to properly comment my functional scheme code. I know how to add a comment of course – you add a ; and put your comment after it. My question is what should I put in my comments, and where should I comment for maximum readability and comprehensability for other programmers reading my code?

Here’s a code snippet I wrote. It’s a function called display-n. It can be called with any number of arguments and outputs each argument to the screen in the order that they are provided.

(define display-n
  (lambda nums
    (letrec ((display-n-inner 
              (lambda (nums)
                (display (car nums))
                (if (not (equal? (cdr nums) (quote ()))
                    (display-n-inner (cdr nums))))))
      (display-n-inner nums))))

Edit: Improved tabbing and replaced '() with (quote ()) to avoid SO messing up the formatting.

I’m just not sure how/where to add comments to make it more understandable. Some scheme code I’ve seen just has comments at the top, which is great if you want to use the code, but not helpful if you want to understand/modify it.

Also – how should I comment macros?

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

    The common style for Lisp comments is

    • Four semicolons for commentary on a whole subsection of a file.
    • Three semicolons for introducing a single procedure.
    • Two semicolons for a description of the expression/procedure definition on the following line.
    • One semicolon for an endline comment.

    Procedure overview comments should probably follow the style of RnRS documens, so to just add comments to your procedure as-is, would look something like

    ;;; Procedure: display-n NUM ...
    ;; Output each argument to the screen in the order they are provided.
    (define 
      display-n (lambda nums
                  (letrec ((display-n-inner (lambda (nums)
                                              (display (car nums))
                                              (if (not (equal? (cdr nums) '()))
                                                  (display-n-inner (cdr nums))))))
                    (display-n-inner nums))))
    

    N.B. I don’t use three semicolons for the whole procedure description, since it screws up fill-paragraph in Emacs.


    Now about the code, I would ditch the whole define-variable-as-a-lambda thing. Yes, I get that this is the “purest” way to define a function, and it makes for a nice consistency with defining procedures are the results of LETs and other procedures, but there’s a reason for syntactic sugar, and it’s to make things more readable. Same for the LETREC—just use an internal DEFINE, which is the same thing but more readable.

    It’s not a huge deal that DISPLAY-N-INNER’s parameter is called NUMS, since the procedure’s so short and DISPLAY-N just hands its NUMS straight to it anyways. “DISPLAY-N-INNER” is sort of a lame name, though. You would give it something with more semantic meaning, or give it a simple name like “ITER” or “LOOP”.

    Now about the logic of the procedure. First, (equal? (cdr nums) '()) is silly, and is better as (null? (cdr nums)). Actually, when you are operating over an entire list, it’s best to make the base case a test of whether the list itself, and not its CDR, is empty. This way the procedure won’t error if you pass it no arguments (unless you want it to do that, but I think it makes more sense for DISPLAY-N to do nothing if it gets nothing). Furthermore, you should test whether to stop the procedure, not whether to continue:

    (define (display-n . nums)
      (define (iter nums)
        (if (null? nums)
            #t  ; It doesn't matter what it returns.
            (begin (display (car nums))
                   (iter (cdr nums)))))
      (iter nums))
    

    But for all that, I would say the the procedure itself is not the best way to accomplish the task it does, since it is too concerned with the details of traversing a list. Instead you would use the more abstract FOR-EACH method to do the work.

    (define (display-n . nums)
      (for-each display nums))
    

    This way, instead of a reader of the procedure getting mired in the details of CARs and CDRs, he can just understand that FOR-EACH will DISPLAY each element of NUMS.

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

Sidebar

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.