Re: [Haskell-cafe] Re: Datatypes - Haskell
ok: On the subject of data types, I've recently seen Haskell code using data Foo ... = Foo { ... } where I would have used newtype instead of data. When is it a good idea to avoid newtype? It depends what's in the ... If its just something with the same representation as an existing type, using a newtype makes sense. If it builds a more complicated single-contructor type, such as many record types, then data is required. -- Don ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Re: Datatypes - Haskell
On Feb 10, 2008 3:40 PM, Mattes Simeon [EMAIL PROTECTED] wrote: Thanks for your help. It was very useful. Though in comparison with C or C++ I can't figure out so clear the syntax. Maybe it has to do with the syntactic Sugar of each Language. I 'll give you a similar example I saw in a book for Haskel The following program just returns the value of the position of a datatype Tuple which can hold one or two elements. data Tuple a b = One a | Two a b tuple1 (One a)= Just a tuple1 (Two a b) = Just a tuple2 (One a) = Nothing tuple2 (Two a b) = Just b The corresponding Version in C++, which seems to be more appropriate, would be I think this is the most native way to do it in C++: template class A, class B class Tuple { public: static TupleA, B *One (A *a) { return new One (a); } static TupleA, B *Two (A *a, B *b) { return new Two (a, b); } virtual A *tuple1 () = 0; virtual B *tuple2 () = 0; }; template class A, class B class One : TupleA, B { public: One (A *a) { this-a = a; } A *tuple1 () { return a; } B *tuple2 () { return NULL; } private: A *a; } template class A, class B class Two: TupleA, B { public: Two (A *a, B *b) { this-a = a; this-b = b} A *tuple1 () { return a; } B *tuple2 () { return b; } private: A *a; B *b; } -- vir http://vir.comtv.ru/ ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Re: Datatypes - Haskell
G'day all. On Feb 10, 2008 3:40 PM, Mattes Simeon [EMAIL PROTECTED] wrote: Though in comparison with C or C++ I can't figure out so clear the syntax. Quoting Victor Nazarov [EMAIL PROTECTED]: I think this is the most native way to do it in C++: Herb Sutter and Andrei Alexandrescu will find you and beat you up if you write this. This is considered more appropriate. (Warning: untested code follows.) #include boost/variant.hpp templatetypename A, typename B class Tuple : public boost::variantA, std::pairA,B { private: typedef std::pairA,B pair_type; typedef boost::variantA, pair_type base_type; struct visitor_A : public boost::static_visitorconst A* { const A* operator()(const A a) { return a; } const A* operator()(const pair_type p) { return p.first; } }; public: Tuple(const A a) : base_type(a) { } Tuple(const A a, const B b) : base_type(pair_type(a,b)) { } const A* tuple1() const { return boost::apply_visitor(visitor_A(), *this); } const B* tuple2() const { const pair_type* p = boost::getpair_type(*this); return p ? p-second : 0; } }; But in this specific case, this might be more appropriate: templatetypename A,typename B class Tuple { A m_a; boost::optionalB m_b; // etc }; Cheers, Andrew Bromage ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Re: Datatypes - Haskell
Mattes Simeon [EMAIL PROTECTED] wrote: Though in comparison with C or C++ I can't figure out so clear the syntax...I seems realy strange, and I'm confused. Surely a solution to this would be to use the standard types of Haskel for tuples and check out each time if I have just a number or a tuple. But this is how somebody thinks in imperative languages. Functional programming is something more... The data declarations in Haskell are well-suited to pattern matching -- you have a compact way to express each alternative, so you can match on that alternative. The C++ way offers a *uniform* interface to every alternative, so you can do if-tests that will type check. The do an if test at runtime way is very general, but basically unoptimizable; whereas pattern matching is very specific, and can be optimized (see page 5 of Luca Cardelli's [Compiling ML]). Sorry for beeing so naive, but although unions, enum, structure are just some tools in C, surely something more in C++, in Haskell they are seem to be a standard. It's certainly true that Haskell elevates certain common data structures to the level of 'native citizens', providing short cut syntax and so forth. Personally, I think C's approach is an example of neglect in this domain, not sparseness; however, at the time C was introduced, I would not have said the same thing at all. -- _jsn [Compiling ML]: http://lucacardelli.name/Papers/CompilingML.pdf ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe