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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:12:37+00:00 2026-05-14T04:12:37+00:00

(define (lcs lst1 lst2) (define (except-last-pair list) (if (pair? (cdr list)) (cons (car list)

  • 0
  (define (lcs lst1 lst2)
   (define (except-last-pair list)
   (if (pair? (cdr list))
   (cons (car list) (except-last-pair (cdr list)))
   '()))
  (define (car-last-pair list)
   (if (pair? (cdr list))
   (car-last-pair (cdr list))
   (car list)))
  (if (or (null? lst1) (null? lst2)) null
   (if (= (car-last-pair lst1) (car-last-pair lst2))
      (append (lcs (except-last-pair lst1) (except-last-pair lst2)) (cons (car-last-pair lst1) '()))
      (**if (> (length (lcs lst1 (except-last-pair lst2))) (length (lcs lst2 (except-last-pair lst1)))) 
          (lcs lst1 (except-last-pair lst2))
          (lcs lst2 (except-last-pair lst1))))))**

I dont want it to run over and over..

Regards,
Superguay

  • 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-14T04:12:38+00:00Added an answer on May 14, 2026 at 4:12 am

    I see that this is supposed to take the longest common subsequence of two lists. As you noted, it’s pretty slow. You will need to take a more complicated approach than just brute-forcing it like this if you want it to work more quickly; the canonical solution is the dynamic programming algorithm described in this Wikipedia article.

    One way you could vastly speed up your solution without totally switching to a different algorithm is through memoization of the results, since as it stands you’ll be computing the same intermediate results over and over as you recurse and drop elements from the ends of the lists.

    EDIT: Well, an easy way to make it a bit faster without much work would be to use a let clause to avoid extra work at the bottom, i.e.

    (if (or (null? lst1) (null? lst2)) null
      (let ((front1 (except-last-pair lst1)) (front2 (except-last-pair lst2)))
        (if (= (car-last-pair lst1) (car-last-pair lst2))       
          (append (lcs front1 front2) (cons (car-last-pair lst1) '()))
          (let ((sub-lcs1 (lcs lst1 front2)) (sub-lcs2 (lcs lst2 front1)))
            (if (> (length sub-lcs1) (length sub-lcs2)) 
              sub-lcs1
              sub-lcs2))))
    

    Hope it’s correct – I don’t have an interpreter handy – but you get the picture.

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

Sidebar

Related Questions

(define a 42) (set! 'a 10) (define a 42) (define (symbol) 'a) (set! (symbol)
How do you define your UserControls as being in a namespace below the project
I want to define something like this in php : $EL = \n<br />\n;
How would you define testing? In the interest of full disclosure, I'm posting this
Can you define a macro that accesses a normal variable, but in a read-only
I try to define a schema for XML documents I receive. The documents look
I'm trying to define a task that emits (using echo) a message when a
In C# we can define a generic type that imposes constraints on the types
Is it possible to define a timestamp column in a MySQL table that will
I need to define a calculated member in MDX (this is SAS OLAP, but

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.