[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: the forward method [dynamic vs. static typing]
Ken Shan writes:
> Let me just note that the Haskell type class system gives you an
> infinite, type-directed family of read functions. If you know how to
> read a Double, you also know how to read a [Double] (i.e., a Double
> list), and [[Double]], and so on. And you can custom-define a data type
> like
>
> data SuperList a = SuperList a (SuperList [a])
>
> whose reading potentially involves all of this infinite family of
> list-reading functions. This infinity distinguishes Haskell type
> classes from, say, C++ templates.
I'm curious what you think about the following.
(Analogous code for operator<< is left to the reader.)
#include <list>
#include <vector>
#include <iostream>
using namespace std;
template <template <typename> class Container, typename T>
ostream &
operator<<(ostream &os, Container<T> const &c)
{
typename Container<T>::const_iterator it = c.begin();
os << "[";
if (it != c.end()) {
for (;;) {
os << *it;
++it;
if (it == c.end()) {
break;
}
os << ",";
}
}
os << "]";
return os;
}
int
main()
{
int pi[6] = { 3, 1, 4, 1, 5, 9 };
int e[6] = { 2, 7, 1, 8, 2, 8 };
vector<int> v(&pi[0], &pi[6]);
list<int> xs(&pi[0], &pi[6]);
list<int> ys(&e[0], &e[6]);
vector<list<int> > lv;
lv.push_back(xs);
lv.push_back(ys);
/*
[3,1,4,1,5,9]
[3,1,4,1,5,9]
[[3,1,4,1,5,9],[2,7,1,8,2,8]]
*/
cout << v << endl;
cout << xs << endl;
cout << lv << endl;
}
--
Franklin