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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:11:56+00:00 2026-05-31T02:11:56+00:00

In Lisp, I have to create a program that does the following (please visit

  • 0

In Lisp, I have to create a program that does the following (please visit link):

http://uva.onlinejudge.org/external/103/10328.html

I have code to create the tree

(defun head-tail (n  &optional (total 0))
  (if (< total n)
      (list(cons 'H (head-tail n (1+ total)))
             (cons 'T (head-tail n (1+ total))))
    nil))

and then code to check the sequence of H = heads

(defun head-search2 (tree n &optional (total 0) (check 0))
  (cond ((null tree)
         check)
        ((listp (first tree))
         (+ (head-search2 (first tree) n total)
            (head-search2 (rest tree) n total check)))
        ((and (eq (first tree) 'H)
              (>= (1+ total) n))
         (head-search2 (rest tree) n (1+ total) 1))
        ((and (eq (first tree) 'H)
              (< (1+ total) n))
         (head-search2 (rest tree) n (1+ total) check))
        ((eq (first tree) 'T)
         (head-search2 (rest tree) n 0 check ))))

and a last function to combine those two

(defun head-check (m n)
  (head-search2(head-tail m) n))

The code is not working with large numbers of trees, any help would be great!

  • 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-31T02:11:57+00:00Added an answer on May 31, 2026 at 2:11 am

    There are two problems:

    1. In the function head-search2, second clause of cond, first recursive call to head-search2 fails to propagate the check down.

    2. Same clause, second recursive call gets (rest tree) as first parameter, which results in an extra layer of list; it should be (second tree) instead.

    That said, you traverse the tree twice: first when constructing, and then counting it. With a little bit more careful thinking, you can save a lot of work traversing it just once, without constructing it explicitly:

    (defun count-n-runs (m n &optional (k n))
      "Count all possible binary sequences with n consecutive 1s."
      (cond ((= 0 n) (expt 2 m))
            ((= 0 m) 0)
            ((+ (count-n-runs (1- m) k k)
                (count-n-runs (1- m) (1- n) k)))))
    

    Rewriting this further for dynamic programming is left as an exercise for the reader. 😉

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

Sidebar

Related Questions

I have a lisp program that needs to run for a long, long time.
Say, if I have a Lisp program, which uses (eval 'sym) and looks it
I have read a lot that LISP can redefine syntax on the fly, presumably
I have been searching everywhere for the following functionality in Lisp, and have gotten
I have read that most languages are becoming more and more like lisp, adopting
I've been learning Lisp to expand my horizons because I have heard that it
I note that Scheme and Lisp (I guess) support circular lists, and I have
I have a fairly involved LispWorks Common Lisp module that sits atop some .NET
Can classes have multiple constructors and/or copy constructors in common-lisp? That is - in
I am trying to get http://www.emacswiki.org/emacs/PrettyGreek working for emacs 21.3.1 on Windows. I have

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.