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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:16:31+00:00 2026-06-14T20:16:31+00:00

I am trying to write a function which compares two lists for homework. When

  • 0

I am trying to write a function which compares two lists for homework. When the function run it should be something like this ;(cmp ‘(cat ?x mat ?x) ‘(cat bat mat bat)) => t ;(cmp ‘(cat ?x mat ?x) ‘(cat bat mat sat)) => nil. meaning that in the first list when equal to ?x and the second ?x return true if both are pointing to the same value.
When I run the program now is giving me “error while parsing arguments to special form if: invalid number of elements” Here is my code if you can give me some feedback. Thanks.

;cmp algorithm
;1 if the both lists are empty return true
;2 if only one of the lists is empty return fasle
;3 compare first of the list1 and the first of list2
;if equal go on to the rest of the list with recursive call else return false   

(defun cmp (list1 list2)
(setq y '())
(setq z '())
(defparameter *counter* 0)
  (cond 
   ((and (null list1) (null list2))
    t 
    )
   ((or (null list1) (null list2))
    nil
    )
    ((or (eq (first list1) (first list2)) 
         (eq (first list1) '?x)  )
     (cmp (rest list1) (rest list2) )

        ;if (first list is equal to '?x)
        ;set the counter to 1
        ;give the value of (first(rest list2)) to y 
        ;if (first list is equal to '?x) again
        ;set the counter to 2
        ;give the value of (first (rest list2)) to z
        ;i need to compare y and z if eq return true

     (if (eq (first list1) '?x) 
        (princ (first list1 ))
        (princ (first(rest list2)))

        (1+ *counter*)
        (set y (first(rest list2)))

        (if (= *counter* 2)
        (set z (first (rest list2)))    
            )       
        )

        (if (= y z) t)      
     )
   (t
    nil)
   )
  )





;(cmp ‘(cat ?x mat ?x) ‘(cat bat mat bat))  =>  t 
  ;(cmp ‘(cat ?x mat ?x) ‘(cat bat mat sat))  =>  nil
  • 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-14T20:16:32+00:00Added an answer on June 14, 2026 at 8:16 pm

    You’re almost there. You’re missing how to match generically on any symbol whose first character is ? and how to pass matches to recursive calls.

    You need to save your matches somewhere between calls. A possible approach is pass them in an optional association list of matches:

    (defun cmp (list1 list2 &optional matches)
      (cond ((and (null list1) (null list2))
             t)
            ((or (null list1) (null list2))
             nil)
            ((and (symbolp (first list1))
                  (plusp (length (symbol-name (first list1))))
                  (eql (char (symbol-name (first list1)) 0) #\?))
             (let ((assoc (assoc (first list1) matches)))
               (cond ((null assoc)
                      (cmp (rest list1) (rest list2)
                           (list* (cons (first list1) (first list2))
                                  matches)))
                     ((eql (cdr assoc) (first list2))
                      (cmp (rest list1) (rest list2) matches)))))
            ((eql (first list1) (first list2))
             (cmp (rest list1) (rest list2) matches))))
    

    A very similar approach to this one which uses a dynamic variable:

    (defvar *matches* '())
    
    (defun cmp (list1 list2)
      (cond ((and (null list1) (null list2))
             t)
            ((or (null list1) (null list2))
             nil)
            ((and (symbolp (first list1))
                  (plusp (length (symbol-name (first list1))))
                  (eql (char (symbol-name (first list1)) 0) #\?))
             (let ((assoc (assoc (first list1) matches)))
               (cond ((null assoc)
                      (let ((*matches* (list* (cons (first list1) (first list2))
                                              *matches*)))
                        (cmp (rest list1) (rest list2))))
                     ((eql (cdr assoc) (first list2))
                      (cmp (rest list1) (rest list2))))))
            ((eql (first list1) (first list2))
             (cmp (rest list1) (rest list2)))))
    

    Both could be called this way:

    > (cmp '(?x b ?x d ?y f ?y h)
           '(a  b c  d  e f g  h))
    nil
    > (cmp '(?x b ?x d ?y f ?y h)
           '(a  b a  d  e f e  h))
    t
    

    However, if you already start with an association list of matches, the first one is called like this:

    > (cmp '(?x ?y)
           '(a  b)
           '((?x . a)))
    t
    

    While the second one is to be used like this:

    > (let ((*matches* '((?x . a))))
        (cmp '(?x ?y)
             '(a  b)))
    t
    

    Exercise: Make cmp always match '? (a symbol whose name is solely the question mark) to anything.

    This may be useful if you want an element to be there but you want to ignore it otherwise.


    Exercise: Make cmp more useful and return the list of found associations instead of t:

    > (cmp '(?x ?y)
           '(a  b))
    ((?x . a)
     (?y . b))
    
    ;;; Assuming option one
    > (cmp '(?x ?y)
           '(a  b)
           '((?x . a)
             (?z . c)))
    ((?x . a)
     (?y . b))
    > (cmp '(?x ?y)
           '(c  b)
           '((?x . a)
             (?z . c)))
    nil
    

    The idea is to return only the found associations, and not the unused ones. So, even though the second test returns non-nil, ?z doesn’t appear in the result.

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

Sidebar

Related Questions

i am trying to write a zoom function which looks something like this: centre
i am trying to write one function which return sql statement like function get_sql($name=0,$date_start=0,$date_end=0)
I am trying to write an operator != function that compares if two complex
I am trying to write a function which returns a string created from two
I'm trying to make it so I can write a function which defines a
I'm trying to write a function that takes multiple arguments, which can come either
I'm trying to write a function where only two method calls (with the methods
I've had problem when trying write a function which has a default value when
I'm trying to write a function which re-uses the implicit conversions which I have
What I am trying is write recursive function which returns the least common divisor

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.