views:

1126

answers:

3

Another hw question, here is what I have so far. My Goal is to make the function part? return true if a list or an item is inside the nested list.

But so far I can only get it working with signal item inside a first order list. (not nested list yet)

(define part?  
  (lambda (item l)
    (and (not (null? l))
         (or (= item (car l))
             (part? item (cdr l))))))

my goal is to have

part? of (A (B)), (((B) A (B)) C) is #f and

part? of (A B), (C (A B) (C)) is #t

Where I should improve on this? How can I make the list to compare with the nested list?

+1  A: 

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.

Dirk
thanks for the hit, right I should use eq? i think. How can I do it recursively ? so that the nested lists are also compared ?
Jonathan
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
+2  A: 

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.

  1. A list may contain an item if the list is not null.

  2. A list contains an item if the car of the list matches the item or if the car of the list contains the item

  3. A list contains an item if the cdr of the list contains the item.

  4. 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.

plinth
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
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
You might want to tweak this function so it works correctly when item is '().
Pillsy
+1  A: 

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))) ...)
SpaceghostAli