Best way to compare parameter members

Discussion of Common Lisp
Post Reply
E S
Posts: 1
Joined: Sun May 15, 2011 7:15 am

Best way to compare parameter members

Post by E S » Sun May 15, 2011 7:37 am

Hi guys, first time poster and newbie lisp coder looking for a little help.

I'm writing a program with several parameters. One of these is the world state, one contains a group of lists called RR, and the other contains just one state called PR. The idea is that the user will choose a list from RR to be used before running the program, the program will be aware of the choice but not the contents of the list at the start. At a certain point in the program the program will be allowed access to the list from RR and will then have to compare it against PR. I'm trying to figure out the best way to do this and could use some advice, before proceeding.

Is there a way to compare two parameters as you would compare two ordinary lists? If so, how would I define which list from RR I was using? Would it be better to copy the list from RR into a variable within world, and then compare it with PR?


On a related topic, is there a funtion similar to the merge funciton that takes the elements of a list that don't appear in another and add them to a seperate list? like this;

Code: Select all

 (function 'list2 (list1 1 2 3 4) (list2 1 2 6 7) )
#(3 4)
I don't care about stuff in list2 that is unique, just stuff in list1. Just checking there isn't a function that already does this before I write my own :)

Thank you very much

JamesF
Posts: 98
Joined: Thu Jul 10, 2008 7:14 pm

Re: Best way to compare parameter members

Post by JamesF » Sun May 15, 2011 7:12 pm

Your question is a little vague, but I'll answer it as best I can.

For any given function, its parameter names are just convenient handles for the values that are passed in, bearing in mind that CL is a pass-by-reference language. If two of those parameters happen to be lists, then they're lists and can be treated as such, including all comparisons that apply to lists.

The vagueness comes in where you refer to the program having several parameters, and ask about performing comparisons on them. Assuming the program comprises several functions, and said parameters are passed to its initial/starting function, all you have to do is have it pass a suitable subset of those parameters to the function that performs the comparison.
It sounds like this program is expected to interact with a user, presenting them with a list of options and then using that input to determine which element of RR it is to compare with PR. For this, you probably want to look into accessor functions for hash-tables and lists, particularly alists and plists. You also want to know that CL isn't especially restrictive - both lists and hash-tables can contain any type of object, including lists made up of other lists.

You seem a little unclear on how RR is to be supplied to the program, and how the program is to interact with it. On the one hand, you're supplying that group of lists as an argument; on the other, you're stating that the program is not aware of RR's contents and that it will only be permitted access to those contents at a predetermined point in its execution. Are you actually supplying that data to the program, or are you merely advising it of the data's location in some external resource (filesystem, URL, database or something of the sort) and then directing it to access a subset of the data in response to the user's input? If you're supplying the data, then the program has access to it and is aware of the contents (leaving aside any quibbling about whether it's the program or the l lisp image that is aware of it, and about the philosophical implications of that term).
I ask that question partly to help you clarify in your own mind what you're doing. I've learned the hard way that it's extremely valuable to start by describing the exact problem you're trying to solve, leaving out all considerations of how you might implement it, and only worrying about the implementation after you know exactly what you need it to do.

nuntius
Posts: 538
Joined: Sat Aug 09, 2008 10:44 am
Location: Newton, MA

Re: Best way to compare parameter members

Post by nuntius » Mon May 16, 2011 9:49 pm

I'll defer to JamesF's answer for the first part of your question.

Here's a simple approach to the second. It should be fine for lists up to moderate length (say 30 or so); after that a different approach might be faster (e.g. sort list2 or put it in a hashtable).

Code: Select all

(let ((list1 (list 1 2 3 4))
      (list2 (list 1 2 6 7)))
  (remove-if (lambda (x) (member x list2)) list1))
-> (3 4)

ander-skirnir
Posts: 2
Joined: Tue May 17, 2011 11:16 am

Re: Best way to compare parameter members

Post by ander-skirnir » Tue May 17, 2011 11:21 am

Code: Select all

(set-difference '(1 2 3 4)
                '(1 2 6 7))
=> (4 3)
(set-difference-in-hyperspec)

Post Reply