Scheme Compare item or list if it's inside the test list (can be nested)?

A b))) I used the cond structure because it fits well with problems of this sort - it just feels right to break the problem out - notice the null check. I wrote same? As a helper function in case you want to write it yourself.

You could just as easily to it this way: (define same?(lambda (a b) (cond ((and (atom? A) (atom? B)) (eq?

A b)) ((and (list? A) (list? B)) (and (same?(car a) (car b)) (same?

(cdr a) (cdr b)))) (f f) ) )) which basically says that two items are the same if they are both atoms and they are eq or they are both lists and the cars are the same and the cdrs are the same, otherwise false You can just as easily rewrite same? As this: (define same?(lambda (a b) (equal? A b))) The point of doing that is that there is a bottleneck in the code - how to determine if two items are the same.

If you break that bottleneck out into its own function, you can replace it with different mechanisms. I know you're not here yet, but you will be at one point: you will also be able to rewrite the code so that the predicate is passed in. Something like this (and more up-to-date scheme programmers, feel free to correct me): (define part-pred?(lambda (same-pred item l) (cond ((null?

L) f) ((or (same-pred item (car l)) (part? Item (car l)))) (t (part? Item (cdr l))) ) )) (define part-eq?

(lambda (item l) (part-pred?'eq? Item l))) (define part-same? (lambda (item l) (part-pred?'same?

Item l))) (define part-equal? (lambda (item l) (part-equal?'equal? Item l))) This has now abstracted the notion of part to be a function that applies the part structural rules and an equality predicate which is supplied by you.

That makes it really easy to change the rules. This will make more sense when you hit mapcar.

DISCLAIMER: I haven't written scheme regularly in 20 years. I think you want to think about this problem in terms of the whole lispy/schemey approach which is to establish your conditions. Given an input, item, you want to find if a list contains that item.

A list may contain an item if the list is not null. A list contains an item if the car of the list matches the item or if the car of the list contains the item A list contains an item if the cdr of the list contains the item. A final question to consider, what is the result of (part?'(a b) '(a b))?

I would write the code like this (define part? (lambda (item l) (cond ((null? L) f) ((or (same?

Item (car l)) (part? Item (car l)))) (t (part? Item (cdr l))) ) )) (define same?

(lambda (a b) (eq? A b))) I used the cond structure because it fits well with problems of this sort - it just feels right to break the problem out - notice the null check. I wrote same?

As a helper function in case you want to write it yourself. You could just as easily to it this way: (define same?(lambda (a b) (cond ((and (atom? A) (atom?

B)) (eq? A b)) ((and (list? A) (list?

B)) (and (same?(car a) (car b)) (same? (cdr a) (cdr b)))) (f f) ) )) which basically says that two items are the same if they are both atoms and they are eq or they are both lists and the cars are the same and the cdrs are the same, otherwise false. You can just as easily rewrite same?

As this: (define same? (lambda (a b) (equal? A b))) The point of doing that is that there is a bottleneck in the code - how to determine if two items are the same.

If you break that bottleneck out into its own function, you can replace it with different mechanisms. I know you're not here yet, but you will be at one point: you will also be able to rewrite the code so that the predicate is passed in. Something like this (and more up-to-date scheme programmers, feel free to correct me): (define part-pred?(lambda (same-pred item l) (cond ((null?

L) f) ((or (same-pred item (car l)) (part? Item (car l)))) (t (part? Item (cdr l))) ) )) (define part-eq?

(lambda (item l) (part-pred? 'eq? Item l))) (define part-same?

(lambda (item l) (part-pred? 'same? Item l))) (define part-equal?

(lambda (item l) (part-equal? 'equal? Item l))) This has now abstracted the notion of part to be a function that applies the part structural rules and an equality predicate which is supplied by you.

That makes it really easy to change the rules. This will make more sense when you hit mapcar.

Thank you so much for the help! I just found out that there is a native function called member that does very similar thing. I just need to make the list into a recursion.

– Jonathan Sep 25 '09 at 14:33 You might want to be careful about using member - very often in early homework problems you are forbidden from using the built-ins so that you get used to doing all the work and internalizing the process. It's like doing long division out by hand instead of using a calculator, if that makes sense. – plinth Sep 25 '09 at 14:44 You might want to tweak this function so it works correctly when item is '().

– Pillsy Sep 25 '09 at 15:13.

The problem with the = function used here is, that it is only defined for numbers. To test arbitrary data for equality, there are the predicates eq? , eqv?

, and equal?. These are listed here from most discriminating (eq? , basically something like a pointer comparison) to least discriminating (equal?

Will consider type and structure). The eqv? Predicate is somewhere in between (type-aware for numbers, otherwise like eq?

For anything else). To compare lists, you will usually use equal?. Edit Details on these predicate can be found in the R6RS.

I think. How can I do it recursively? So that the nested lists are also compared?

– Jonathan Sep 25 '09 at 13:49 Think about it: you might actually have two lists, which look the same but are distinct objects. What would eq? Return for these objects?

What would equal? Return? – Dirk Sep 25 '09 at 14:17.

Your solution is missing the idea, since you're dealing with nested lists you need to check if each item in each list is a list itself, if it is then check if the given list is part of the other or part of the rest of the list if not then you need to check the first items are equal and if the rest of the given list is part of the other. (define part? Item l (cond (and (list?(car item)) (not (list?(car l))) ...) (and (not (list?(car item))) (list?(car l)) ...) (and (not (list?(car item))) (not (list?(car l))) ...) (and (list?(car item)) (list?(car l))) ...).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions