Typed Python?

Christophe Cavalaria chris.cavalaria at free.fr
Mon Jul 5 15:15:57 EDT 2004


Ville Vainio wrote:

>>>>>> "Jacek" == Jacek Generowicz <jacek.generowicz at cern.ch> writes:
> C/C++:
> 
> ------
> 
> #include <stdio.h>
> typedef struct listCell * list;
> struct listCell {
>   int  first;
>   list rest;
> };
> bool guest (int x, list l) {
>   if (l == NULL)
>     return false;
>   else if (x == (l -> first))
>     return true;
>   else
>     return guest (x, l -> rest);
> }
> int main (int argc, char ** argv) {
>   list l1, l2, l3 = NULL;  int  x;
>   l1 = (list) malloc (sizeof (struct listCell));
>   l2 = (list) malloc (sizeof (struct listCell));
>   l2 -> first = 3;  l2 -> rest = l3;
>   l1 -> first = 2;  l1 -> rest = l2;
>   scanf ("%d", &x);
>   printf ("%d\n", member (x, l1));
> }
> ----------

Some people need to be shot ! This is not C or C++ code, it is C code and
considered very bad C++ code. No wonder that C++ get so much bad press with
such slander.

The *real* C++ version :

#include <list>
#include <string>
#include <algorithm>

using namespace std;

bool guest(string m, list<string> l)
{
    return find(l.begin(),l.end(),m) != l.end();
}

// And now the testing code

#include <iostream>

void try_find(string m, list<string> l)
{
    if(guest(m,l))
        cout << "Found   " << m << '\n';
    else
        cout << "Missing " << m << '\n';
}

int main()
{
    list<string> l;
    l.push_back("Hi");
    l.push_back("There");
    l.push_back("You");

    try_find("Hi",l);
    try_find("Yes",l);
    try_find("You",l);
}




More information about the Python-list mailing list