Live data from Hacker News

Haskell's effect on my C++ : exploit the type system

vallettaventures.com

31–40 of 57 posts

Re: Haskell's effect on my C++ : exploit the type system

#31
post #20
post #7

Do people really still make the `=` vs `==` mistake? GCC -Wall has had "warning: suggest parentheses around assignment used as truth value" since as long as I can remember.

It's the one mistake that everyone seems to cite when complaining about C, and I've literally never seen it happen in all my years of programming. Right up there with fall-through cases.

I have made both mistakes.

Re: Haskell's effect on my C++ : exploit the type system

#32
post #3
post #2

If your only example to show C++ verbosity fails to compile and is also patently wrong, you are not making a good point. (The constructor is private, the variable naming prevents short accessors such as Rectangle::width()). Better: struct Rectangle { double x,y, width, length; }; int main() { Rectangle r = {0., 0., 10., 10. }; return 0; } This even compiles in C++03. If you really insist on private members, you will…

bookmarked: http://bartoszmilewski.com/2011/07/11/monads-in-c/ http://bartoszmilewski.com/2010/11/29/understanding-c-concep...

Also found a def of type classes that i like: automatic instantiation + constraint propagation

http://blog.garillot.net/post/15147165154/every-single-fresh...

Re: Haskell's effect on my C++ : exploit the type system

#33

My somewhat biased stance: functional programming languages make you progress as a programmer quickly. Becoming great requires deliberate practice and intrinsically hard problems. So you learn programming and CS about 5-10 times as fast in a functional language. This is part of why enterprise Java/C++ devs plateau at a skill level of 1.2 (even into their gray-haired years) while ML and Haskell programmers can reach 1…

I agree with one side of your message.

There are very few people who have just learnt functional programming. I have met a few in academia, and they tend to be as "bad" as the pure Java/C++ programmers, but in a different way. They have trouble understanding how their code will boil down to CPU instructions, and so have trouble with performance, particularly (unsuprisingly) mutating algorithms, which are vital for performance in some situations.

After I have defined a high-performance algorithm, they often want to modify it to make it "more beautiful" (which means functional) and in the process destroy the performance.

Also, I had quite a lot of exposure to Haskell, and I haven't found C++ hate. I got some Haskell hate, because doing some things (mutating algorithms and bit fiddling) were really, really hard, while I found most of the things I loved from Haskell I could do in C++, with care.

I work on low-level algorithm design in A.I., where performance is everything and there are many interlocking, low-level and complex algorithms and data structures. I know that other problems might well have very different characteristics. For example, I have no real ideal how Haskell vs C++ would compare for the average web app for example (or of course, something like Ruby/Python which seem to be much more popular).

Re: Haskell's effect on my C++ : exploit the type system

#34
post #27

Earlier quoted context omitted.

Yes, I know, but there is no way to enforce it. This a classic "noob" mistake with macros - using a macro argument more than once.

That's why in C++ people seem to generally recommend inline functions instead. Not only do the arguments only get evaluated once, but the whole thing is type safe too. Of course, people still misuse macros.

Macros, by definition, are always misused, complete the languages point of view. They exist to let the programmer get the job done when the language falls short.

Re: Haskell's effect on my C++ : exploit the type system

#35
post #16

One of the more clever not so well known C++ type system hacks I have seen is the way Ogre3D (a graphics engine for games) handle Degrees versus radians. Basically they have a class for each and have overloaded the operators so that you can mix and match operations freely (e.g you get a radian result from some computation which you can then add twenty degrees to simply by saying + Degree(20) and have the C++ type sys…

Do you mean "members are final" or "classes are final"? A final member is constant, which has nothing to do with inheritance. A final class has no subclasses.

Re: Haskell's effect on my C++ : exploit the type system

#36
post #16

One of the more clever not so well known C++ type system hacks I have seen is the way Ogre3D (a graphics engine for games) handle Degrees versus radians. Basically they have a class for each and have overloaded the operators so that you can mix and match operations freely (e.g you get a radian result from some computation which you can then add twenty degrees to simply by saying + Degree(20) and have the C++ type sys…

[deleted]

Re: Haskell's effect on my C++ : exploit the type system

#37

map and fold are available in C++, though they're called std::transform (in ) and std::accumulate (in ). Totally agree with the author's point, the C++ I write nowadays is heavily influenced by my Scala/Haskell/Lisp exposure and is much better for it.

Does 'accumulate' work on non-'numeric' types?

Part of the beauty of Haskell is the aggressive effort to define each function on the most general type it applies to (and then specialize to concrete types during compilation, a la C++, when performance is requested via a pragma), leading to a rich hierarchy of tiny type classes (type classes are similar to C++ abstract classes)

Re: Haskell's effect on my C++ : exploit the type system

#38

That first example is terrible. C doesn't guarantee that the arguments to a macro are evaluated only once. At best, you might be doing the computation twice. At worst (if the function has internal state), you could get different answers for the comparison and the assignment. The non Haskell-style is better!

Do note that calling functions with the unsafe side effects is not Haskell-style, as that expression is not even possible in Haskell without literally typing word "unsafe" into your program at least twice. It is supposedly "Haskell-style" only in the OP's incorrect description.

Re: Haskell's effect on my C++ : exploit the type system

#39
post #27

Earlier quoted context omitted.

Technically, the code is wrong only if f() is not pure.

Yes, I know, but there is no way to enforce it. This a classic "noob" mistake with macros - using a macro argument more than once.

I feel pretty safe assuming a pure function in a blog post about the advantages of immutability, really.

Re: Haskell's effect on my C++ : exploit the type system

#40
post #37

map and fold are available in C++, though they're called std::transform (in ) and std::accumulate (in ). Totally agree with the author's point, the C++ I write nowadays is heavily influenced by my Scala/Haskell/Lisp exposure and is much better for it.

Does 'accumulate' work on non-'numeric' types? Part of the beauty of Haskell is the aggressive effort to define each function on the most general type it applies to (and then specialize to concrete types during compilation, a la C++, when performance is requested via a pragma), leading to a rich hierarchy of tiny type classes (type classes are similar to C++ abstract classes)

Yeah, it's a real fold, just misleadingly named:

  int main() {
    string s[] = { "a", "b", "c" };
    // will print "abc"
    cout 
I think a closer C++ equivalent for typeclasses are specialized template functions--for example, if I define:

  template string accumulate(InputIterator first,
    InputIterator last, string init);
then my call to accumulate(s, s+3, string("")) will use that specialization.
Post reply on HN