Thread: Recursive List - CDR - CAR ??

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    15

    Recursive List - CDR - CAR ??

    I can't find any tutorials or info on Recursive List - cdr / car. I dont know what to return in member, any hints

    Code:
    bool eq(list atom1, list atom2)
     // returns true if atom1 & atom2 are the saem atoms
     // otherwise false
    
    
    
    bool member(list p, list q)
      //q is an atomp; p is a list of atoms
      // member returns true if q is a member
    Code:
    bool eq(list atom1, list atom2)
      if (atom1 == atom2)
         return true;
         return false;
    
    
    
    bool member(list p, list q)
       if(is_null(p)
          return false;
    
       return ??????

  2. #2
    Dump Truck Internet valis's Avatar
    Join Date
    Jul 2005
    Posts
    357
    use lisp?
    Not sure if this is what you mean, but:
    Code:
    for each member of q
    {
      if current member is p
        return true
    }
    return false

  3. #3
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Something like this nearly-C pseudocode perhaps?

    Code:
    bool member(list p, list q)
        if (is_null(p))
            return false
    
        if (eq(q, car(p)))
            return true
    
        return member(cdr(p), q)
    In C and C++, the || operator is a branching operator. You could replace your '?????' with eq(q, car(p)) || member(cdr(p), q).

    You do know what car and cdr represent, right? Car is the contained element in the first node of the list; cdr is the rest of the list.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 09:01 AM
  3. Linked List
    By jpipitone in forum C Programming
    Replies: 4
    Last Post: 03-30-2003, 09:27 PM
  4. i really need help here
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 04-09-2002, 10:47 PM
  5. I need help with an algorithm
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 04-07-2002, 07:58 PM