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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:49:35+00:00 2026-06-12T20:49:35+00:00

Need to write a union function in lisp that takes two lists as arguments

  • 0

Need to write a union function in lisp that takes two lists as arguments and returns a list that is the union of the two with no repeats. Order should be consistent with those of the input lists

For example: if inputs are ‘(a b c) and ‘(e c d) the result should be ‘(a b c e d)

Here is what I have so far

(defun stable-union (x y)
  (cond
   ((null x) y)
   ((null y) x))
  (do ((i y (cdr i))
       (lst3 x (append lst3 
                       (cond
                        ((listp i) 
                         ((null (member (car i) lst3)) (cons (car i) nil) nil))
                        (t (null (member i lst3)) (cons i nil) nil)))))
        ((null (cdr i)) lst3)))

My error is that there is an “illegal function object” with the segment (null (member (car i) lst3))

Advice?

  • 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-06-12T20:49:36+00:00Added an answer on June 12, 2026 at 8:49 pm

    You’ve got your parens all jumbled-up:

    (defun stable-union (x y)
      (cond
       ((null x) y)
       ((null y) x)  )     END OF COND form - has no effect
    
      (do ((i y (cdr i))
          ^^
           (lst3 x (append lst3 
                           (cond
                            ((listp i) 
                             (  (null (member (car i) lst3)) 
                             ^^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ called as a function
                                 (cons (car i) nil)           with two arguments
                                 nil  ) )
                                     ^^ 
                            (t                         NEXT 3 forms have no effect
                               (null (member i lst3))
                               (cons i nil)
                               nil           )))) )
                                                 ^^
            ((null (cdr i)) lst3)))
    

    Here’s your code as you probably intended it to be, with corrected parenthesization and some ifs added where needed:

    (defun stable-union (x y)
      (cond
       ((null x) y)
       ((null y) x)
       (t
        (do ((i y (cdr i))
             (lst3 x (append lst3 
                       (cond
                         ((listp i) 
                           (if (null (member (car i) lst3))
                               (cons (car i) nil)
                               nil))
                         (t 
                           (if (null (member i lst3))
                               (cons i nil)
                               nil))))))
            ((null (cdr i)) lst3)))))
    

    There are still problems with this code. Your do logic is wrong, it skips the first element in y if it contains just one element. And you call append all the time whether it is needed or not. Note that calling (append lst3 nil) makes a copy of top-level cons cells in lst3, entirely superfluously.

    Such long statements as you have there are usually placed in do body, not inside the update form for do‘s local variable.


    But you can use more specialized forms of do, where appropriate. Here it is natural to use dolist. Following “wvxvw”‘s lead on using hash-tables for membership testing, we write:

    (defun stable-union (a b &aux (z (list nil)))
      (let ((h (make-hash-table))
            (p z))
        (dolist (i a)
          (unless (gethash i h)
            (setf (cdr p) (list i) p (cdr p))
            (setf (gethash i h) t)))
        (dolist (i b (cdr z))
          (unless (gethash i h)
            (setf (cdr p) (list i) p (cdr p))
            (setf (gethash i h) t)))))
    

    using a technique which I call “head-sentinel” (z variable pre-initialized to a singleton list) allows for a great simplification of the code for the top-down list building at a cost of allocating one extra cons cell.

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

Sidebar

Related Questions

I need to write a C++ code coverage program that takes in another C++
I have to write a query that performs a union between two tables with
I need to write function for split string with two chars. Ex: Hyderabad Hyd,Test
I need to write a program that prints 0.(03) for input 1 and 33.
I need to write a query that calls a recursive query many times. I
Given two arrays int arr1[n] int arr2[m] where n > m Need to write
I need to write a rule that blocks a mysql query.. like RewriteCond %{QUERY_STRING}
I need to write a function which could create any type object. It receives
The application need write file's last modification date. void Dater(String DateFile) { File file
I need to write a script in Matlab, which will read some data from

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.